summaryrefslogtreecommitdiff
path: root/vm/memory.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/memory.go
parentc30e7f5f99f9ad4d9552b645d7b89d7385972f99 (diff)
downloadevm-ba2b9c8a1bb1876b6eb4c9783fde798b19de4418.tar.gz
init
Diffstat (limited to 'vm/memory.go')
-rw-r--r--vm/memory.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/vm/memory.go b/vm/memory.go
new file mode 100644
index 0000000..0a9dd2c
--- /dev/null
+++ b/vm/memory.go
@@ -0,0 +1,39 @@
+package main
+
+import (
+ "github.com/holiman/uint256"
+ "log"
+)
+
+type Memory []byte
+
+// store 256b word at offset in memory
+func (m *Memory) sw(ost uint64, val *uint256.Int) {
+ if ost + 32 > uint64(len(*m)) {
+ *m = append(*m, make([]byte, ost + 32 - uint64(len(*m)))...)
+ }
+
+ bytes := val.Bytes32()
+ copy((*m)[ost:], bytes[:])
+}
+
+// store byte at offset in memory
+func (m *Memory) sb(ost uint64, val byte) {
+ if ost + 1 > uint64(len(*m)) {
+ *m = append(*m, make([]byte, ost + 1 - uint64(len(*m)))...)
+ }
+
+ (*m)[ost] = val
+}
+
+// loads 256b word fron offset in memory
+func (m *Memory) ld(ost uint64) []byte {
+ if ost + 32 > uint64(len(*m)) {
+ log.Fatal("trying to load out of memory")
+ }
+
+ r := make([]byte, 32)
+ copy(r, (*m)[ost:ost+32])
+ return r
+}
+