Skip to content

Commit

Permalink
Default exec command logger to error level
Browse files Browse the repository at this point in the history
  • Loading branch information
smoynes committed Nov 23, 2023
1 parent b6e586e commit 05ca103
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
10 changes: 7 additions & 3 deletions internal/cli/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"flag"
"fmt"
"io"
"log/slog"
"os"
"time"

Expand All @@ -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 {
Expand All @@ -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))
})
Expand Down
5 changes: 4 additions & 1 deletion internal/vm/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}

Expand Down
33 changes: 33 additions & 0 deletions internal/vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 05ca103

Please sign in to comment.