From c7d559ef790d89a6c4f7c6ce57daa964988d99ef Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 26 Aug 2018 12:36:17 +0200 Subject: ... --- core.go | 45 ++++++++++++---------- core_test.go | 2 + parse.go | 117 +++++++++++++++++++++++++++++----------------------------- parse_test.go | 2 + stack.go | 8 ++++ 5 files changed, 96 insertions(+), 78 deletions(-) diff --git a/core.go b/core.go index 624d4b3..a0044fc 100644 --- a/core.go +++ b/core.go @@ -17,6 +17,11 @@ type Console interface { Len() uint16 } +type io interface { + readAt(addr uint16) uint16 + writeAt(addr uint16, value uint16) +} + // Core of J1 Forth CPU // // 33 deep × 16 bit data stack @@ -130,7 +135,7 @@ func (c *Core) Execute(ins Instruction) error { } c.st0 = c.d.pop() case ALU: - if v.RtoPC { + if v.Ret { c.pc = c.r.peek() >> 1 } if v.NtoAtT { @@ -158,40 +163,40 @@ var boolValue = map[bool]uint16{ true: ^uint16(0), } -func (c *Core) newST0(opcode uint16) uint16 { +func (c *Core) newST0(opcode Opcode) uint16 { T, N, R := c.st0, c.d.peek(), c.r.peek() switch opcode { - case opT: // T + case OpT: // T return T - case opN: // N + case OpN: // N return N - case opTplusN: // T+N + case OpTplusN: // T+N return T + N - case opTandN: // T&N + case OpTandN: // T&N return T & N - case opTorN: // T|N + case OpTorN: // T|N return T | N - case opTxorN: // T^N + case OpTxorN: // T^N return T ^ N - case opNotT: // ~T + case OpNotT: // ~T return ^T - case opNeqT: // N==T + case OpNeqT: // N==T return boolValue[N == T] - case opNleT: // N>T + case OpNrshiftT: // N>>T return N >> (T & 0xf) - case opTminus1: // T-1 - return T - 1 - case opR: // R (rT) + case OpNlshiftT: // N<> 8) & 15, - RtoPC: v&(1<<12) != 0, - TtoN: v&(1<<7) != 0, - TtoR: v&(1<<6) != 0, - NtoAtT: v&(1<<5) != 0, + Opcode: Opcode((v >> 8) & 15), + Ret: v&(1<<7) != 0, + Mod: Modifier((v >> 4) & 7), Rdir: expand[(v>>2)&3], Ddir: expand[(v>>0)&3], } @@ -122,19 +133,11 @@ func newALU(v uint16) ALU { func isALU(v uint16) bool { return v&(7<<13) == 3<<13 } func (v ALU) value() uint16 { - ret := v.Opcode << 8 - if v.RtoPC { - ret |= 1 << 12 - } - if v.TtoN { + ret := uint16(v.Opcode) << 8 + if v.Ret { ret |= 1 << 7 } - if v.TtoR { - ret |= 1 << 6 - } - if v.NtoAtT { - ret |= 1 << 5 - } + ret |= uint16(v.Mod) << 4 ret |= uint16(v.Rdir&3) << 2 ret |= uint16(v.Ddir&3) << 0 return ret @@ -142,44 +145,42 @@ func (v ALU) value() uint16 { func (v ALU) compile() uint16 { return v.value() | (3 << 13) } +type Opcode uint16 + const ( - opT = 0x0 - opN = 0x1 - opTplusN = 0x2 - opTandN = 0x3 - opTorN = 0x4 - opTxorN = 0x5 - opNotT = 0x6 - opNeqT = 0x7 - opNleT = 0x8 - opNrshiftT = 0x9 - opTminus1 = 0xa - opR = 0xb - opAtT = 0xc - opNlshiftT = 0xd - opDepth = 0xe - opNuleT = 0xf + OpT Opcode = 0 + OpN Opcode = 1 + OpTplusN Opcode = 2 + OpTandN Opcode = 3 + OpTorN Opcode = 4 + OpTxorN Opcode = 5 + OpNotT Opcode = 6 + OpNeqT Opcode = 7 + OpNleT Opcode = 8 + OpNrshiftT Opcode = 9 + OpNlshiftT Opcode = 10 + OpR Opcode = 11 + OpAtT Opcode = 12 + OpIoAtT Opcode = 13 + OpStatus Opcode = 14 + OpNuLeT Opcode = 15 ) var opcodeNames = []string{ "T", "N", "T+N", "T∧N", "T∨N", "T⊻N", "¬T", "N=T", - "N