Skip to content

Commit

Permalink
Add INCX and INCY instructions.
Browse files Browse the repository at this point in the history
  • Loading branch information
aleury committed Jan 15, 2024
1 parent 0775772 commit eb0f7a5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
8 changes: 8 additions & 0 deletions gmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const (
OpNOOP
OpOUTA
OpINCA
OpINCX
OpINCY
OpDECA
OpDECX
OpDECY
Expand Down Expand Up @@ -68,6 +70,8 @@ var opcodes = map[string]Word{
"NOOP": OpNOOP,
"OUTA": OpOUTA,
"INCA": OpINCA,
"INCX": OpINCX,
"INCY": OpINCY,
"DECA": OpDECA,
"DECX": OpDECX,
"DECY": OpDECY,
Expand Down Expand Up @@ -134,6 +138,10 @@ func (g *Machine) Run() {
binary.Write(g.Out, binary.BigEndian, g.A)
case OpINCA:
g.A++
case OpINCX:
g.X++
case OpINCY:
g.Y++
case OpDECA:
g.A--
case OpDECX:
Expand Down
26 changes: 26 additions & 0 deletions gmachine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,32 @@ func TestINCA(t *testing.T) {
}
}

func TestINCX(t *testing.T) {
t.Parallel()
g := gmachine.New(nil)
err := assembleAndRunFromString(g, "INCX")
if err != nil {
t.Fatal("didn't expect an error:", err)
}
var wantX gmachine.Word = 1
if wantX != g.X {
t.Errorf("want X value %d, got %d", wantX, g.X)
}
}

func TestINCY(t *testing.T) {
t.Parallel()
g := gmachine.New(nil)
err := assembleAndRunFromString(g, "INCY")
if err != nil {
t.Fatal("didn't expect an error:", err)
}
var wantY gmachine.Word = 1
if wantY != g.Y {
t.Errorf("want Y value %d, got %d", wantY, g.Y)
}
}

func TestIllegalInstruction(t *testing.T) {
t.Parallel()
g := gmachine.New(nil)
Expand Down
2 changes: 1 addition & 1 deletion testdata/gc.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ SETA 42
OUTA

-- want --
0000000 0000 0000 0000 0b00 0000 0000 0000 2a00
0000000 0000 0000 0000 0d00 0000 0000 0000 2a00
0000010 0000 0000 0000 0300
0000018
2 changes: 2 additions & 0 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ var opcodes = map[string]TokenType{
"NOOP": OPCODE,
"OUTA": OPCODE,
"INCA": OPCODE,
"INCX": OPCODE,
"INCY": OPCODE,
"DECA": OPCODE,
"DECX": OPCODE,
"DECY": OPCODE,
Expand Down
3 changes: 3 additions & 0 deletions token/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func TestLookupIdent(t *testing.T) {
{"NOOP", token.OPCODE},
{"OUTA", token.OPCODE},
{"INCA", token.OPCODE},
{"INCX", token.OPCODE},
{"INCY", token.OPCODE},
{"DECA", token.OPCODE},
{"DECX", token.OPCODE},
{"DECY", token.OPCODE},
Expand All @@ -29,6 +31,7 @@ func TestLookupIdent(t *testing.T) {
{"JUMP", token.OPCODE},
{"JXNZ", token.OPCODE},
{"X", token.REGISTER},
{"Y", token.REGISTER},
{"test", token.IDENT},
{".test", token.IDENT},
}
Expand Down

0 comments on commit eb0f7a5

Please sign in to comment.