-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathemu_zpu.lua
More file actions
executable file
·69 lines (56 loc) · 1.42 KB
/
Copy pathemu_zpu.lua
File metadata and controls
executable file
·69 lines (56 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env lua
-- ZPU Emulator: Example usage.
local arg = arg or {...}
local fname = arg[1]
if not fname then
error("Need filename")
end
local f, err = io.open(fname, "rb")
if err then error(err) end
local memsz = 0x80000
-- Load bitops
local bitops = require("bitops")
-- Load ZPU
local zpu = require("zpu")
-- Install bitops
zpu.set_bit32(bitops)
-- Load ZPU emulates and apply them
local zpu_emulates = require("zpu_emus")
zpu:apply(zpu_emulates)
local memlib = require("memlib")
-- Memory: ROM, RAM and peripherals.
local t = f:read(memsz)
local rom = memlib.new("rostring", t)
f:close()
local mem = memlib.new("rwoverlay32", rom, memsz)
-- Address handlers/Peripherals
local addr_handlers = {}
addr_handlers[0x80000024] = function(comp, method, i, v)
-- UART(O)
if method == "get32be" then return 0x100 end
if method == "set32be" then
io.write(string.char(bitops.band(v, 0xFF)))
io.flush()
return
end
end
addr_handlers[0x80000028] = function(comp, method, i, v)
-- UART(I)
if method == "get32be" then
local inp = io.read(1)
local ret = (inp and string.byte(inp)) or 0
return bitops.bor(ret, 0x100)
end
end
local comp = memlib.compose(mem, addr_handlers)
local function get32(zpu_inst, i, v)
return comp:get32be(i)
end
local function set32(zpu_inst, i, v)
return comp:set32be(i, v)
end
-- Get ZPU instance and set up.
local zpu_inst = zpu.new(get32, set32)
zpu_inst.rSP = memsz
while zpu_inst:run() do
end