From 211e7dfae79cf13b2302e67e1b8266ca46114dd3 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Wed, 24 Jan 2018 02:07:03 +0100 Subject: unexport Instruction methods --- core.go | 16 ++++++++++------ parse.go | 24 ++++++++++++------------ parse_test.go | 2 +- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/core.go b/core.go index 5b08cd9..e66d3f9 100644 --- a/core.go +++ b/core.go @@ -10,8 +10,9 @@ import ( const memSize = 0x4000 -var ErrStop = errors.New("stop") +var errStop = errors.New("stop") +// Console i/o type Console interface { Read() uint16 Write(uint16) @@ -27,6 +28,7 @@ type Core struct { tty Console // console i/o } +// New core func New(con Console) *Core { return &Core{tty: con} } @@ -69,7 +71,7 @@ func (c *Core) writeAt(addr, value uint16) error { case 0xf000: // key c.tty.Write(value) case 0xf002: // bye - return ErrStop + return errStop } return nil } @@ -98,24 +100,26 @@ func (c *Core) Run() { } } +// Decode instruction func (c *Core) Decode() Instruction { return Decode(c.memory[c.pc]) } +// Eval instruction func (c *Core) Eval(ins Instruction) error { c.pc++ switch v := ins.(type) { case Lit: c.d.push(c.st0) - c.st0 = v.Value() + c.st0 = v.value() case Jump: - c.pc = v.Value() + c.pc = v.value() case Call: c.r.push(c.pc << 1) - c.pc = v.Value() + c.pc = v.value() case Cond: if c.st0 == 0 { - c.pc = v.Value() + c.pc = v.value() } c.st0 = c.d.pop() case ALU: diff --git a/parse.go b/parse.go index 1dc4d08..3a6bd8d 100644 --- a/parse.go +++ b/parse.go @@ -21,8 +21,8 @@ func Decode(v uint16) Instruction { // Instruction interface type Instruction interface { - Value() uint16 - Compile() uint16 + value() uint16 + compile() uint16 } // Lit is a literal @@ -30,32 +30,32 @@ type Lit uint16 func newLit(v uint16) Lit { return Lit(v &^ uint16(1<<15)) } func (v Lit) String() string { return fmt.Sprintf("LIT %0.4X", uint16(v)) } -func (v Lit) Value() uint16 { return uint16(v) } -func (v Lit) Compile() uint16 { return v.Value() | (1 << 15) } +func (v Lit) value() uint16 { return uint16(v) } +func (v Lit) compile() uint16 { return v.value() | (1 << 15) } // Jump is an unconditional branch type Jump uint16 func newJump(v uint16) Jump { return Jump(v &^ uint16(7<<13)) } func (v Jump) String() string { return fmt.Sprintf("UBRANCH %0.4X", uint16(v<<1)) } -func (v Jump) Value() uint16 { return uint16(v) } -func (v Jump) Compile() uint16 { return v.Value() } +func (v Jump) value() uint16 { return uint16(v) } +func (v Jump) compile() uint16 { return v.value() } // Cond is a conditional branch type Cond uint16 func newCond(v uint16) Cond { return Cond(v &^ uint16(7<<13)) } func (v Cond) String() string { return fmt.Sprintf("0BRANCH %0.4X", uint16(v<<1)) } -func (v Cond) Value() uint16 { return uint16(v) } -func (v Cond) Compile() uint16 { return v.Value() | (1 << 13) } +func (v Cond) value() uint16 { return uint16(v) } +func (v Cond) compile() uint16 { return v.value() | (1 << 13) } // Call procedure type Call uint16 func newCall(v uint16) Call { return Call(v &^ uint16(7<<13)) } func (v Call) String() string { return fmt.Sprintf("CALL %0.4X", uint16(v<<1)) } -func (v Call) Value() uint16 { return uint16(v) } -func (v Call) Compile() uint16 { return v.Value() | (2 << 13) } +func (v Call) value() uint16 { return uint16(v) } +func (v Call) compile() uint16 { return v.value() | (2 << 13) } // ALU instruction type ALU struct { @@ -80,7 +80,7 @@ func newALU(v uint16) ALU { } } -func (v ALU) Value() uint16 { +func (v ALU) value() uint16 { ret := v.Opcode << 8 if v.RtoPC { ret |= 1 << 12 @@ -99,7 +99,7 @@ func (v ALU) Value() uint16 { return ret } -func (v ALU) Compile() uint16 { return v.Value() | (3 << 13) } +func (v ALU) compile() uint16 { return v.value() | (3 << 13) } func expand(v uint16) int8 { if v&2 != 0 { diff --git a/parse_test.go b/parse_test.go index e69774b..9652b60 100644 --- a/parse_test.go +++ b/parse_test.go @@ -39,7 +39,7 @@ func TestDecode(t *testing.T) { if ins != tc.ins { t.Errorf("got %v, want %v", ins, tc.ins) } - if v := ins.Compile(); v != tc.bin { + if v := ins.compile(); v != tc.bin { t.Errorf("got %v, want %v", v, tc.bin) } }) -- cgit v1.2.3