Commit 854d220a authored by George Hotz's avatar George Hotz

reads and writes in go

parent a2a4b486
......@@ -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
......
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
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment