Skip to content

Commit

Permalink
Add RunContext & VirtualMachine structres to Write your own VM se…
Browse files Browse the repository at this point in the history
…ction (#12)

* Add RunContext

* Add VM structure
  • Loading branch information
fmoletta authored Jul 31, 2023
1 parent 9a14c0b commit 7850e11
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,37 @@ func (m *MemorySegmentManager) LoadData(ptr Relocatable, data *[]MaybeRelocatabl
}
```

### RunContext

The RunContext keeps track of the vm's registers. Cairo VM only has 3 registers:

- The program counter `Pc`, which points to the next instruction to be executed.
- The allocation pointer `Ap`, pointing to the next unused memory cell.
- The frame pointer `Fp`, pointing to the base of the current stack frame. When a new function is called, `Fp` is set to the current `Ap` value. When the function returns, `Fp` goes back to its previous value.

We can represent it like this:

```go
type RunContext struct {
Pc memory.Relocatable
Ap memory.Relocatable
Fp memory.Relocatable
}
```

### VirtualMachine
With all of these types and structures defined, we can build our VM:

```go
type VirtualMachine struct {
runContext RunContext
currentStep uint
segments memory.MemorySegmentManager
}
```

To begin coding the basic execution functionality of our VM, we only need these basic fields, we will be adding more fields as we dive deeper into this guide.

### Builtins

TODO
Expand Down

0 comments on commit 7850e11

Please sign in to comment.