You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
883 B
38 lines
883 B
package hal
|
|
|
|
import "fmt"
|
|
|
|
type Program map[int64]*ProgrammedInstruction
|
|
|
|
type Module struct {
|
|
Accumulator float64
|
|
ProgramStorage Program
|
|
Register []float64
|
|
}
|
|
|
|
func (h *Module) ProgramCounter() int64 {
|
|
return int64(h.Register[0])
|
|
}
|
|
|
|
func (h *Module) increaseProgramCounter() {
|
|
h.Register[0]++
|
|
}
|
|
|
|
func (h *Module) Step() error {
|
|
instruction := h.ProgramStorage[h.ProgramCounter()]
|
|
h.increaseProgramCounter()
|
|
if instruction == nil {
|
|
return fmt.Errorf("instruction not found for program counter = %d", h.ProgramCounter())
|
|
}
|
|
return instruction.Execute(h)
|
|
}
|
|
|
|
func NewHALModule(program Program, registerSize uint64) (*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),
|
|
}, nil
|
|
}
|
|
|