Skip to content

Commit 089d5d0

Browse files
committed
Moved error state from CPU to stepTrace
1 parent 712bd3e commit 089d5d0

File tree

8 files changed

+172
-175
lines changed

8 files changed

+172
-175
lines changed

cpu.go

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ type Processor struct {
9797
buf [64]byte // general purpose buffer
9898
op uint16 // current opcode
9999
runState RunState // current state
100-
err error // most recent error
101100
handlers [256]TrapHandler // registered trap handlers
102101
}
103102

@@ -131,53 +130,50 @@ func (c *Processor) Reset() {
131130

132131
// Run executes any program loaded into memory, starting from the program
133132
// counter value, running until completion.
134-
func (c *Processor) Run() error {
133+
func (c *Processor) Run() (err error) {
135134
if c.M == nil {
136135
return errNoProgram
137136
}
138-
c.err = nil
139137
c.runState = RunStateRunning
140-
for c.err == nil && c.runState == RunStateRunning {
141-
c.Step()
138+
for err == nil && c.runState == RunStateRunning {
139+
err = c.Step()
142140
}
143141
c.runState = RunStateStopped
144-
return c.err
142+
return
145143
}
146144

147145
// Step executes the single instruction located at the address specified by the
148146
// program counter register.
149-
func (c *Processor) Step() error {
150-
// TODO: the returned error must be a traceableError.
151-
147+
func (c *Processor) Step() (err error) {
152148
// TODO: consider prefetching a whole page of instructions and measure
153149

154150
// read from program pointer into op
155-
if _, c.err = c.M.Read(int(c.PC), c.buf[0:2]); c.err != nil {
156-
return c.err
151+
if _, err = c.M.Read(int(c.PC), c.buf[0:2]); err != nil {
152+
return
157153
}
158154
c.op = uint16(c.buf[0])<<8 + uint16(c.buf[1])
159155

160156
if c.op == 0 {
161157
// TODO: 0 is a valid instruction. Find a way to terminate programs
162158
// without buffer overrun.
163159
c.runState = RunStateStopped
164-
return c.err
160+
return
165161
}
166162

167163
// dispatch opcode to function
168164
fn := dispatch(c.op)
169165
if fn == nil {
170-
c.err = newTraceableError(c.PC, c.op, errBadOpcode)
171-
return c.err
166+
err = newTraceableError(c.PC, c.op, errBadOpcode)
167+
return
172168
}
173169
t := fn(c)
174170

175171
// BUG: trace data might be corrupted by an execution error.
176172
c.trace(t)
177-
if c.err != nil {
178-
c.err = newTraceableError(t.addr, c.op, c.err)
173+
if t.err != nil {
174+
err = newTraceableError(t.addr, c.op, t.err)
179175
}
180-
return c.err
176+
return
181177
}
182178

183179
// Stop instructs the processor to stop processing instructions.
@@ -382,7 +378,7 @@ func (c *Processor) readWord(ea uint16) (n uint16, opr string, err error) {
382378
opr = fmt.Sprintf("$%X", addr)
383379

384380
case 0x04: // immediate word
385-
n, c.err = c.M.Word(int(c.PC))
381+
n, err = c.M.Word(int(c.PC))
386382
c.PC += 2
387383
opr = fmt.Sprintf("#$%X", wordToInt32(n))
388384
}
@@ -459,7 +455,7 @@ func (c *Processor) readLong(ea uint16) (n uint32, opr string, err error) {
459455
opr = fmt.Sprintf("$%X", addr)
460456

461457
case 0x04: // immediate long
462-
n, c.err = c.M.Long(int(c.PC))
458+
n, err = c.M.Long(int(c.PC))
463459
c.PC += 4
464460
opr = fmt.Sprintf("#$%X", int32(n))
465461
}
@@ -477,17 +473,17 @@ func (c *Processor) readBytes(ea uint16, b []byte) (opr string, err error) {
477473
return
478474

479475
case 0x02: // memory address
480-
_, c.err = c.M.Read(int(c.A[reg]), b)
476+
_, err = c.M.Read(int(c.A[reg]), b)
481477
opr = fmt.Sprintf("(A%d)", reg)
482478

483479
case 0x03: // memory address with post-increment
484-
_, c.err = c.M.Read(int(c.A[reg]), b)
480+
_, err = c.M.Read(int(c.A[reg]), b)
485481
c.A[reg] += 4
486482
opr = fmt.Sprintf("(A%d)+", reg)
487483

488484
case 0x04: // memory address with pre-decrement
489485
c.A[reg] -= 4
490-
_, c.err = c.M.Read(int(c.A[reg]), b)
486+
_, err = c.M.Read(int(c.A[reg]), b)
491487
opr = fmt.Sprintf("-(A%d)", reg)
492488

493489
case 0x05: // memory address with displacement
@@ -498,7 +494,7 @@ func (c *Processor) readBytes(ea uint16, b []byte) (opr string, err error) {
498494
}
499495
c.PC += 2
500496
addr := int(c.A[reg]) + int(d)
501-
_, c.err = c.M.Read(addr, b)
497+
_, err = c.M.Read(addr, b)
502498
opr = fmt.Sprintf("($%X,A%d)", d, reg)
503499

504500
case 0x07: // other
@@ -514,7 +510,7 @@ func (c *Processor) readBytes(ea uint16, b []byte) (opr string, err error) {
514510
return
515511
}
516512
c.PC += 2
517-
_, c.err = c.M.Read(int(addr), b)
513+
_, err = c.M.Read(int(addr), b)
518514
opr = fmt.Sprintf("$%X", addr)
519515

520516
case 0x01: // absolute long
@@ -524,7 +520,7 @@ func (c *Processor) readBytes(ea uint16, b []byte) (opr string, err error) {
524520
return
525521
}
526522
c.PC += 4
527-
_, c.err = c.M.Read(int(addr), b)
523+
_, err = c.M.Read(int(addr), b)
528524
opr = fmt.Sprintf("$%X", addr)
529525
}
530526
}

0 commit comments

Comments
 (0)