Merge remote-tracking branch 'origin/master'

# Conflicts:
#	cmd/root.go
#	hal/module.go
master
kolaente 5 years ago
commit e06a93d89d
Signed by: kolaente
GPG Key ID: F40E70337AB24C9B
  1. 7
      cmd/root.go
  2. 4
      hal/instructions.go
  3. 24
      hal/module.go

@ -52,6 +52,9 @@ func runRootCommand(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
hal.NewHALModule(program, 256, debug)
return nil
module, err := hal.NewHALModule(program, 256, 2, debug)
if err != nil {
return err
}
return module.Run()
}

@ -59,8 +59,8 @@ var InstructionStop = &Instruction{
var InstructionOut = &Instruction{
Name: "OUT",
Execute: func(module *Module) {
// TODO implement
ExecuteWithOperand: func(module *Module, operand float64) {
module.IO[int64(operand)] = module.accumulator
},
}

@ -7,20 +7,21 @@ const maxInstructions = 1000
type Program map[int64]*ProgrammedInstruction
type Module struct {
Accumulator float64
ProgramStorage Program
Register []float64
accumulator float64
programStorage Program
register []float64
IO []float64
isStopped bool
debugEnabled bool
}
func (h *Module) ProgramCounter() int64 {
return int64(h.Register[0])
return int64(h.register[0])
}
func (h *Module) increaseProgramCounter() {
h.Register[0]++
h.register[0]++
}
func (h *Module) debug(format string, args ...interface{}) {
@ -31,15 +32,15 @@ func (h *Module) debug(format string, args ...interface{}) {
}
func (h *Module) Step() {
instruction := h.ProgramStorage[h.ProgramCounter()]
instruction := h.programStorage[h.ProgramCounter()]
h.increaseProgramCounter()
if instruction == nil {
return
}
h.debug("Instruction: %s", instruction.Instruction.Name)
h.debug("Accumulator before: %d", h.Accumulator)
h.debug("Accumulator before: %d", h.accumulator)
instruction.Execute(h)
h.debug("Accumulator after: %d", h.Accumulator)
h.debug("Accumulator after: %d", h.accumulator)
}
func (h *Module) Run() error {
@ -52,13 +53,14 @@ func (h *Module) Run() error {
return nil
}
func NewHALModule(program Program, registerSize uint64, debug bool) (*Module, error) {
func NewHALModule(program Program, registerSize uint64, ioSize uint64, debug bool) (*Module, error) {
if registerSize <= 10 {
return nil, fmt.Errorf("register size must be greater then 10 [ registerSize = %d ]", registerSize)
}
return &Module{
ProgramStorage: program,
Register: make([]float64, registerSize),
programStorage: program,
register: make([]float64, registerSize),
IO: make([]float64, ioSize),
debugEnabled: debug,
}, nil
}

Loading…
Cancel
Save