@@ -97,7 +97,6 @@ type Processor struct {
97
97
buf [64 ]byte // general purpose buffer
98
98
op uint16 // current opcode
99
99
runState RunState // current state
100
- err error // most recent error
101
100
handlers [256 ]TrapHandler // registered trap handlers
102
101
}
103
102
@@ -131,53 +130,50 @@ func (c *Processor) Reset() {
131
130
132
131
// Run executes any program loaded into memory, starting from the program
133
132
// counter value, running until completion.
134
- func (c * Processor ) Run () error {
133
+ func (c * Processor ) Run () ( err error ) {
135
134
if c .M == nil {
136
135
return errNoProgram
137
136
}
138
- c .err = nil
139
137
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 ()
142
140
}
143
141
c .runState = RunStateStopped
144
- return c . err
142
+ return
145
143
}
146
144
147
145
// Step executes the single instruction located at the address specified by the
148
146
// 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 ) {
152
148
// TODO: consider prefetching a whole page of instructions and measure
153
149
154
150
// 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
157
153
}
158
154
c .op = uint16 (c .buf [0 ])<< 8 + uint16 (c .buf [1 ])
159
155
160
156
if c .op == 0 {
161
157
// TODO: 0 is a valid instruction. Find a way to terminate programs
162
158
// without buffer overrun.
163
159
c .runState = RunStateStopped
164
- return c . err
160
+ return
165
161
}
166
162
167
163
// dispatch opcode to function
168
164
fn := dispatch (c .op )
169
165
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
172
168
}
173
169
t := fn (c )
174
170
175
171
// BUG: trace data might be corrupted by an execution error.
176
172
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 )
179
175
}
180
- return c . err
176
+ return
181
177
}
182
178
183
179
// Stop instructs the processor to stop processing instructions.
@@ -382,7 +378,7 @@ func (c *Processor) readWord(ea uint16) (n uint16, opr string, err error) {
382
378
opr = fmt .Sprintf ("$%X" , addr )
383
379
384
380
case 0x04 : // immediate word
385
- n , c . err = c .M .Word (int (c .PC ))
381
+ n , err = c .M .Word (int (c .PC ))
386
382
c .PC += 2
387
383
opr = fmt .Sprintf ("#$%X" , wordToInt32 (n ))
388
384
}
@@ -459,7 +455,7 @@ func (c *Processor) readLong(ea uint16) (n uint32, opr string, err error) {
459
455
opr = fmt .Sprintf ("$%X" , addr )
460
456
461
457
case 0x04 : // immediate long
462
- n , c . err = c .M .Long (int (c .PC ))
458
+ n , err = c .M .Long (int (c .PC ))
463
459
c .PC += 4
464
460
opr = fmt .Sprintf ("#$%X" , int32 (n ))
465
461
}
@@ -477,17 +473,17 @@ func (c *Processor) readBytes(ea uint16, b []byte) (opr string, err error) {
477
473
return
478
474
479
475
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 )
481
477
opr = fmt .Sprintf ("(A%d)" , reg )
482
478
483
479
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 )
485
481
c .A [reg ] += 4
486
482
opr = fmt .Sprintf ("(A%d)+" , reg )
487
483
488
484
case 0x04 : // memory address with pre-decrement
489
485
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 )
491
487
opr = fmt .Sprintf ("-(A%d)" , reg )
492
488
493
489
case 0x05 : // memory address with displacement
@@ -498,7 +494,7 @@ func (c *Processor) readBytes(ea uint16, b []byte) (opr string, err error) {
498
494
}
499
495
c .PC += 2
500
496
addr := int (c .A [reg ]) + int (d )
501
- _ , c . err = c .M .Read (addr , b )
497
+ _ , err = c .M .Read (addr , b )
502
498
opr = fmt .Sprintf ("($%X,A%d)" , d , reg )
503
499
504
500
case 0x07 : // other
@@ -514,7 +510,7 @@ func (c *Processor) readBytes(ea uint16, b []byte) (opr string, err error) {
514
510
return
515
511
}
516
512
c .PC += 2
517
- _ , c . err = c .M .Read (int (addr ), b )
513
+ _ , err = c .M .Read (int (addr ), b )
518
514
opr = fmt .Sprintf ("$%X" , addr )
519
515
520
516
case 0x01 : // absolute long
@@ -524,7 +520,7 @@ func (c *Processor) readBytes(ea uint16, b []byte) (opr string, err error) {
524
520
return
525
521
}
526
522
c .PC += 4
527
- _ , c . err = c .M .Read (int (addr ), b )
523
+ _ , err = c .M .Read (int (addr ), b )
528
524
opr = fmt .Sprintf ("$%X" , addr )
529
525
}
530
526
}
0 commit comments