Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
whampson committed Jun 3, 2019
1 parent 6c223d9 commit 2be6968
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
1 change: 1 addition & 0 deletions include/emu/lc3.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
*/
#define A_TVT 0x0000 /* Trap Vector Table */
#define A_IVT 0x0200 /* Interrupt Vector Table */
#define A_START 0x0400 /* initial PC value (start of execution) */
#define A_SSP 0x3000 /* default supervisor stack pointer */
#define A_USP 0xFE00 /* default user stack pointer */
#define A_KBSR 0xFE00 /* keyboard status register */
Expand Down
41 changes: 19 additions & 22 deletions src/emu/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,12 @@
#define STATE_MASK_PRIV 0x08
#define STATE_MASK_INT 0x10

#define INITIAL_STATE 18

/*
* Microsequencer control state.
*/
struct ctl_state {
struct micro_op {
uint16_t ird : 1; /* 1 = get next state form IR*/
uint16_t cond : 3; /* next state conditional flags */
uint16_t j : 6; /* next state number */
Expand All @@ -115,7 +117,7 @@ struct ctl_state {
* Next state table.
* Unused states: 26, 46, 53, 55, 57, 61, 63
*/
static const struct ctl_state ctl_rom[] = {
static const struct micro_op ctl_rom[] = {
/* 0-3 */ { 0, COND_BR, 18 }, { 0, COND_NONE, 18 }, { 0, COND_NONE, 29 }, { 0, COND_NONE, 24 },
/* 4-7 */ { 0, COND_ADDR, 20 }, { 0, COND_NONE, 18 }, { 0, COND_NONE, 25 }, { 0, COND_NONE, 23 },
/* 8-11 */ { 0, COND_PRIV, 36 }, { 0, COND_NONE, 18 }, { 0, COND_NONE, 56 }, { 0, COND_NONE, 60 },
Expand Down Expand Up @@ -178,21 +180,16 @@ void cpu_reset(void)
{
memset(&cpu, 0, sizeof(struct lc3cpu));

cpu.state = 18; /* start in first FETCH state */
reg_w(R_6, A_SSP); /* R6 <- Supervisor Stack Pointer */
SET_Z(1); /* set Zero flag */
SET_CE(1); /* set Clock Enable bit */

/* "Hack" to get the CPU to start executing OS code.
TODO: reset vector? could do something like jump to the address stored at
M[0]
*/
cpu.pc = 0x0400;
cpu.state = INITIAL_STATE;
cpu.pc = A_START;
reg_w(R_6, A_SSP);
SET_Z(1);
SET_CE(1);
}

void cpu_tick(void)
{
/* Go to next state */
/* Execute current state operation and determine next state. */
state_table[cpu.state]();
cpu.state = next_state();
}
Expand Down Expand Up @@ -270,28 +267,28 @@ static inline void reg_w(int n, lc3word data)
*/
inline int next_state(void)
{
struct ctl_state ctl;
struct micro_op m_op;
int next_state;

ctl = ctl_rom[cpu.state];
if (ctl.ird) {
m_op = ctl_rom[cpu.state];
if (m_op.ird) {
return OPCODE();
}

next_state = ctl.j;
if (ctl.cond == COND_MEM && mem_ready()) {
next_state = m_op.j;
if (m_op.cond == COND_MEM && mem_ready()) {
next_state |= STATE_MASK_MEM;
}
if ( ctl.cond == COND_BR && cpu.ben) {
if (m_op.cond == COND_BR && cpu.ben) {
next_state |= STATE_MASK_BR;
}
if (ctl.cond == COND_ADDR && IR_11()) {
if (m_op.cond == COND_ADDR && IR_11()) {
next_state |= STATE_MASK_ADDR;
}
if ( ctl.cond == COND_PRIV && PRIVILEGE()) {
if (m_op.cond == COND_PRIV && PRIVILEGE()) {
next_state |= STATE_MASK_PRIV;
}
if (ctl.cond == COND_INT && cpu.intf) {
if (m_op.cond == COND_INT && cpu.intf) {
next_state |= STATE_MASK_INT;
}

Expand Down

0 comments on commit 2be6968

Please sign in to comment.