package vm
import "fmt"
const memSizeFlash = 255
const memSizeRAM = 64
const memSizeStack = 64
// VMachine - vurtual machine structure
type VMachine struct {
flash [memSizeFlash]uint8 // ROM array
ram [memSizeRAM]uint8 // RAM array
stack [memSizeStack]int32 // Stack array
PC uint16 // Programm counter
SC uint16 // Stack counter
IR uint16 // Interrupt address
opcodes [128]func() bool // Pointers on methods
}
func (v *VMachine) tick() bool {
funcID := v.flash[v.PC]
next := v.opcodes[funcID]()
fmt.Println(funcID, next, v.PC)
v.PC++
return next
}
func (v *VMachine) init() {
v.opcodes[0x00] = v.oFin
v.opcodes[0x01] = v.oNoop
}
// Load new bytecode to flash and reset registers
func (v *VMachine) Load(opcodes []uint8) {
v.init()
flash := v.flash[:]
copy(flash, opcodes)
v.PC = 0x0000
v.IR = memSizeFlash - 1
return
}
// Interrupt ticks and call interrupt function
func (v *VMachine) Interrupt() {
return
}
// Run algorithm
func (v *VMachine) Run() {
for {
next := v.tick()
if !next {
break
}
}
fmt.Println(v.flash)
return
}
func (v *VMachine) oFin() bool {
return false
}
func (v *VMachine) oNoop() bool {
return true
}
func Create(a, b int8) *Test {
t := new(Test)
t.Init(a, b)
return t
}