Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
854d220a
Commit
854d220a
authored
Sep 26, 2021
by
George Hotz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
reads and writes in go
parent
a2a4b486
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
27 deletions
+47
-27
MIPS.sol
contracts/MIPS.sol
+7
-1
README
mipsevm/README
+11
-0
main.go
mipsevm/main.go
+29
-26
No files found.
contracts/MIPS.sol
View file @
854d220a
...
...
@@ -46,14 +46,17 @@ contract MIPS {
// decode
// register fetch
uint32 storeAddr;
uint32 rs;
uint32 rt;
if (opcode != 2 && opcode != 3) { // J-type: j and jal have no register fetch
// R-type or I-type (stores rt)
rs = m.ReadMemory(stateHash, REG_OFFSET + ((insn >> 19) & 0x7C));
storeAddr = REG_OFFSET + ((insn >> 14) & 0x7C);
if (opcode == 0) {
// R-type (stores rd)
rt = m.ReadMemory(stateHash, REG_OFFSET + ((insn >> 14) & 0x7C));
storeAddr = REG_OFFSET + ((insn >> 9) & 0x7C);
}
}
...
...
@@ -67,10 +70,13 @@ contract MIPS {
}
// execute
execute(insn, rs, rt, mem);
uint32 val =
execute(insn, rs, rt, mem);
// write back
stateHash = m.WriteMemory(stateHash, storeAddr, val);
stateHash = m.WriteMemory(stateHash, REG_PC, pc+4);
return stateHash;
}
// TODO: move pure testable stuff to LibMIPS.sol
...
...
mipsevm/README
0 → 100644
View file @
854d220a
Running MIPS processor on chain
We considered using
https://github.com/cartesi/machine-solidity-step
but it's around 5000 lines!
* We don't have any virtual memory
* We don't support any CSRs
* MIPS is two files, MIPS + MIPSMemory
cartesi also has a second version of the emulator in Rust. we use the EVM one in both places
\ No newline at end of file
mipsevm/main.go
View file @
854d220a
...
...
@@ -90,8 +90,10 @@ type jsoncontract struct {
DeployedBytecode
string
`json:"deployedBytecode"`
}
var
ram
[]
byte
var
regs
[
4096
]
byte
//var ram []byte
//var regs [4096]byte
var
ram
=
make
(
map
[
uint64
](
uint32
))
func
opStaticCall
(
pc
*
uint64
,
interpreter
*
vm
.
EVMInterpreter
,
scope
*
vm
.
ScopeContext
)
([]
byte
,
error
)
{
// Pop gas. The actual gas is in interpreter.evm.callGasTemp.
...
...
@@ -107,38 +109,39 @@ func opStaticCall(pc *uint64, interpreter *vm.EVMInterpreter, scope *vm.ScopeCon
// Get arguments from the memory.
args
:=
scope
.
Memory
.
GetPtr
(
int64
(
inOffset
.
Uint64
()),
int64
(
inSize
.
Uint64
()))
addr
:=
common
.
BytesToHash
(
args
[
4
:
])
.
Big
()
.
Uint64
()
var
nret
int64
if
addr
>=
0xc0000000
{
addr
-=
0xc0000000
nret
=
(
int64
(
regs
[
addr
])
<<
24
)
|
(
int64
(
regs
[
addr
+
1
])
<<
16
)
|
(
int64
(
regs
[
addr
+
2
])
<<
8
)
|
(
int64
(
regs
[
addr
+
3
])
<<
0
)
addr
+=
0xc0000000
}
else
{
nret
=
(
int64
(
ram
[
addr
])
<<
24
)
|
(
int64
(
ram
[
addr
+
1
])
<<
16
)
|
(
int64
(
ram
[
addr
+
2
])
<<
8
)
|
(
int64
(
ram
[
addr
+
3
])
<<
0
)
if
args
[
0
]
==
98
{
// read
addr
:=
common
.
BytesToHash
(
args
[
4
:
])
.
Big
()
.
Uint64
()
nret
:=
ram
[
addr
]
//scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
ret
:=
common
.
BigToHash
(
big
.
NewInt
(
int64
(
nret
)))
.
Bytes
()
fmt
.
Println
(
"HOOKED READ!"
,
fmt
.
Sprintf
(
"%x = %x"
,
addr
,
nret
))
scope
.
Memory
.
Set
(
retOffset
.
Uint64
(),
retSize
.
Uint64
(),
ret
)
}
else
if
args
[
0
]
==
184
{
addr
:=
common
.
BytesToHash
(
args
[
0x24
:
0x44
])
.
Big
()
.
Uint64
()
dat
:=
common
.
BytesToHash
(
args
[
0x44
:
0x64
])
.
Big
()
.
Uint64
()
fmt
.
Println
(
"HOOKED WRITE!"
,
fmt
.
Sprintf
(
"%x = %x"
,
addr
,
dat
))
ram
[
addr
]
=
uint32
(
dat
)
}
//scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))
ret
:=
common
.
BigToHash
(
big
.
NewInt
(
nret
))
.
Bytes
()
fmt
.
Println
(
"HOOKED!"
,
returnGas
,
fmt
.
Sprintf
(
"%x = %x"
,
addr
,
nret
))
scope
.
Memory
.
Set
(
retOffset
.
Uint64
(),
retSize
.
Uint64
(),
ret
)
//scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
//return ret
scope
.
Contract
.
Gas
+=
returnGas
return
ret
,
nil
// what is the return value here?
return
common
.
Hash
{}
.
Bytes
(),
nil
}
func
main
()
{
fmt
.
Println
(
"hello"
)
ram
,
_
=
ioutil
.
ReadFile
(
"test/add.bin"
)
dat
,
_
:=
ioutil
.
ReadFile
(
"test/add.bin"
)
for
i
:=
0
;
i
<
len
(
dat
);
i
+=
4
{
ram
[
uint64
(
i
)]
=
uint32
(
dat
[
i
])
<<
24
|
uint32
(
dat
[
i
+
1
])
<<
16
|
uint32
(
dat
[
i
+
2
])
<<
8
|
uint32
(
dat
[
i
+
3
])
<<
0
}
/*var parent types.Header
database := state.NewDatabase(parent)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment