summaryrefslogtreecommitdiff
path: root/vm/vm.go
diff options
context:
space:
mode:
authorstefan <stefan@s00.xyz>2023-05-24 08:32:08 -0400
committerstefan <stefan@s00.xyz>2023-05-24 08:32:08 -0400
commitba2b9c8a1bb1876b6eb4c9783fde798b19de4418 (patch)
tree9b49307112b0f489189cf65c1b50dd281f8e430f /vm/vm.go
parentc30e7f5f99f9ad4d9552b645d7b89d7385972f99 (diff)
downloadevm-ba2b9c8a1bb1876b6eb4c9783fde798b19de4418.tar.gz
init
Diffstat (limited to 'vm/vm.go')
-rw-r--r--vm/vm.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/vm/vm.go b/vm/vm.go
new file mode 100644
index 0000000..209ab71
--- /dev/null
+++ b/vm/vm.go
@@ -0,0 +1,45 @@
+package main
+
+import (
+ "fmt"
+ "github.com/holiman/uint256"
+)
+
+type Evm struct {
+ code Rom
+ stack Stack
+ memory Memory
+ pc uint64
+ stopped bool
+}
+
+func NewEvm(_code []byte) *Evm {
+ return &Evm{
+ pc: 0,
+ stopped: true,
+ stack : Stack{},
+ code: _code,
+ }
+}
+
+func (vm *Evm) Start() {
+ vm.stopped = false
+ for !(vm.stopped) {
+ op := vm.code.Fetch(&(vm.pc), 1)[0]
+ fmt.Printf("pc: %d | opcode: %x -> string: %s\n", vm.pc, op, Instructions[op].name)
+ if op >= PUSH1 && op <= PUSH32 {
+ nb := op - PUSH1 + 1
+ fmt.Printf("pushing %d byte value to the stack!\n", vm.pc, nb)
+ b := vm.code.Fetch(&(vm.pc), uint64(nb))
+ x := uint256.NewInt(0)
+ x = x.SetBytes(b)
+ vm.stack.Push(x)
+ } else {
+ vm.Execute(op)
+ }
+ }
+}
+
+func (vm *Evm) Execute(op byte) {
+ Instructions[op].handler(vm)
+}