aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2018-01-09 00:36:27 +0100
committerDimitri Sokolyuk <demon@dim13.org>2018-01-09 00:36:27 +0100
commit15eda60829ae0b4b084fe93bf419bf014a82a21c (patch)
tree57d0aee77a051de00d0220c5160ac6094d615b0a
parent9b4b8bd1910787b4380404ac7315ae65763dcac7 (diff)
extract read/write mem
-rw-r--r--eval.go49
-rw-r--r--parse.go1
2 files changed, 29 insertions, 21 deletions
diff --git a/eval.go b/eval.go
index d4d6f90..072c976 100644
--- a/eval.go
+++ b/eval.go
@@ -79,6 +79,30 @@ func (j1 *J1) pop() uint16 {
return v
}
+func (j1 *J1) write(addr, value uint16) {
+ switch addr {
+ case 0xf000: // key
+ fmt.Fprintf(j1.console, "%c", value)
+ case 0xf002: // bye
+ j1.Reset()
+ default:
+ j1.memory[addr>>1] = value
+ }
+}
+
+func (j1 *J1) read(addr uint16) uint16 {
+ switch addr {
+ case 0xf000: // tx!
+ var b uint16
+ fmt.Fscanf(j1.console, "%c", &b)
+ return b
+ case 0xf001: // ?rx
+ return 1
+ default:
+ return j1.memory[addr>>1]
+ }
+}
+
func (j1 *J1) eval(ins Instruction) {
j1.pc++
switch v := ins.(type) {
@@ -98,8 +122,10 @@ func (j1 *J1) eval(ins Instruction) {
if v.RtoPC {
j1.pc = j1.rstack[j1.rsp] >> 1
}
+ if v.NtoAtT {
+ j1.write(j1.st0, j1.dstack[j1.dsp])
+ }
st0 := j1.newST0(v.Opcode)
- n := j1.dstack[j1.dsp]
j1.dsp += v.Ddir
j1.rsp += v.Rdir
if v.TtoN {
@@ -108,16 +134,6 @@ func (j1 *J1) eval(ins Instruction) {
if v.TtoR {
j1.rstack[j1.rsp] = j1.st0
}
- if v.NtoAtT {
- switch j1.st0 {
- case 0xf000: // key
- fmt.Fprintf(j1.console, "%c", n)
- case 0xf002: // bye
- j1.Reset()
- default:
- j1.memory[j1.st0>>1] = n
- }
- }
j1.st0 = st0
}
}
@@ -163,16 +179,7 @@ func (j1 *J1) newST0(opcode uint16) uint16 {
case opR: // R (rT)
return R
case opAtT: // [T]
- switch T {
- case 0xf000: // tx!
- var b uint16
- fmt.Fscanf(j1.console, "%c", &b)
- return b
- case 0xf001: // ?rx
- return 1
- default:
- return j1.memory[T>>1]
- }
+ return j1.read(T)
case opNlshiftT: // N<<T
return N << (T & 0xf)
case opDepth: // depth (dsp)
diff --git a/parse.go b/parse.go
index 2ab0801..1dc4d08 100644
--- a/parse.go
+++ b/parse.go
@@ -22,6 +22,7 @@ func Decode(v uint16) Instruction {
// Instruction interface
type Instruction interface {
Value() uint16
+ Compile() uint16
}
// Lit is a literal