diff --git a/internal/cli/cmd/exec.go b/internal/cli/cmd/exec.go index cbf10e5..1bd669f 100644 --- a/internal/cli/cmd/exec.go +++ b/internal/cli/cmd/exec.go @@ -6,7 +6,6 @@ import ( "flag" "fmt" "io" - "log/slog" "os" "time" @@ -18,13 +17,17 @@ import ( ) func Executor() cli.Command { - exec := &executor{log: log.DefaultLogger()} + exec := &executor{ + log: log.DefaultLogger(), + logLevel: log.Error, + } + return exec } type executor struct { - logLevel slog.Level log *log.Logger + logLevel log.Level } func (executor) Description() string { @@ -42,6 +45,7 @@ Runs an executable in the emulator.`) func (ex *executor) FlagSet() *cli.FlagSet { fs := flag.NewFlagSet("exec", flag.ExitOnError) + fs.Func("loglevel", "set log `level`", func(s string) error { return ex.logLevel.UnmarshalText([]byte(s)) }) diff --git a/internal/vm/ops.go b/internal/vm/ops.go index 698d256..2f518aa 100644 --- a/internal/vm/ops.go +++ b/internal/vm/ops.go @@ -198,7 +198,10 @@ func (op *addImm) Decode(vm *LC3) { } func (op *addImm) Execute() { - op.vm.REG[op.dr] = Register(int16(op.vm.REG[op.sr]) + int16(op.lit)) + operand := int16(op.vm.REG[op.sr]) + lit := int16(op.lit) + + op.vm.REG[op.dr] = Register(operand + lit) op.vm.PSR.Set(op.vm.REG[op.dr]) } diff --git a/internal/vm/vm_test.go b/internal/vm/vm_test.go index d2bb299..5351da6 100644 --- a/internal/vm/vm_test.go +++ b/internal/vm/vm_test.go @@ -314,6 +314,39 @@ func TestInstructions(tt *testing.T) { } }) + tt.Run("ADDIMM zeroed", func(tt *testing.T) { + var ( + t = NewTestHarness(tt) + cpu = t.Make() + ) + + _ = cpu.Mem.store(Word(cpu.PC), 0b0001_000_000_1_00000|(-9&0x1f)) + cpu.REG[R0] = 9 + + err := cpu.Step() + if err != nil { + t.Error(err) + } + + if op := cpu.IR.Opcode(); op != ADD { + t.Errorf("instr: %s, want: %04b, got: %04b", + cpu.IR, AND, op) + } + + oper := cpu.Decode() + t.Logf("oper: %#+v", oper) + + if cpu.REG[R0] != 0x0000 { + t.Errorf("r0 incorrect, want: %s, got: %s", + Register(0x0000), cpu.REG[R0]) + } + + if !cpu.PSR.Zero() { + t.Errorf("cond incorrect, want: %s, got: %s", + StatusNegative, cpu.PSR) + } + }) + tt.Run("LD", func(tt *testing.T) { var ( t = NewTestHarness(tt)