-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpu.rb
110 lines (94 loc) · 1.54 KB
/
cpu.rb
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#this file really should be commented better
require 'math8'
class Z80clock
attr_accessor :m
attr_accessor :t
def initialize
@m = 0
@t = 0
end
end
class Z80registers
attr_accessor :a
attr_accessor :b
attr_accessor :c
attr_accessor :d
attr_accessor :e
attr_accessor :h
attr_accessor :l
attr_accessor :f
attr_accessor :ix
attr_accessor :iy
attr_accessor :i
attr_accessor :r
attr_accessor :pc
attr_accessor :sp
attr_accessor :m
attr_accessor :t
def initialize
@a = 0
@b = 0
@c = 0
@d = 0
@e = 0
@h = 0
@l = 0
@f = 0
@pc =0
@sp =0
@m = 0
@t = 0
@ix =0
@iy =0
@i = 0
@r = 0
end
end
class Z80memory
def initialize
@memory = Array.new(65536,0)
end
def rb(addr)
@memory[addr]
end
def wb(addr,value)
@memory[addr] = value
end
def load(mem)
i = 0
mem.each { |x| @memory[i] = x; i+=1; }
end
end
class Z80cpu
attr_accessor :clock
attr_accessor :registers
attr_accessor :memory
SIGN = 128
ZERO = 64
HC = 16
OVER = 4
SUB = 2
CARRY = 1
def initialize
@clock = Z80clock.new
@registers = Z80registers.new
@memory = Z80memory.new
@halted = false
end
def halt
@halted = true
end
def load(mem)
@memory.load(mem)
end
def run
while not @halted do
op = @memory.rb[@registers.pc]
@registers.pc += 1
execute(op)
@registers.pc = @registers.pc & 65535
@clock.m += @registers.m
@clock.t += @registers.t
end
end
end