default regs, log/trace macros, print mgba-style state

This commit is contained in:
Matthew Berry 2020-09-14 23:28:50 -07:00
parent fbacc2cf9f
commit 238076dda3
2 changed files with 38 additions and 6 deletions

View file

@ -4,24 +4,28 @@ class CPU
@pipeline = Deque(Word).new 2
def initialize(@gba : GBA)
@r[0] = 0x08000000
@r[1] = 0x000000EA
@r[13] = 0x03007F00
@r[15] = 0x08000000
@cpsr = 0x6000001F
end
def fill_pipeline : Nil
while @pipeline.size < 2
puts "Fetch pc: #{hex_str @r[15]}, instr: #{hex_str @gba.bus.read_word @r[15]}, type: #{Instr.from_hash hash_instr @gba.bus.read_word @r[15]}"
log "Fetch pc: #{hex_str @r[15]}, instr: #{hex_str @gba.bus.read_word @r[15]}, type: #{Instr.from_hash hash_instr @gba.bus.read_word @r[15]}"
@pipeline << @gba.bus.read_word @r[15]
@r[15] &+= 4
end
end
def clear_pipeline : Nil
puts "Clearing pipeline"
log "Clearing pipeline"
@pipeline.clear
end
def check_cond(cond : Int) : Bool
case 0xF_u8 & cond & 0xF
case cond & 0xF
when 0x0 then bit?(@cpsr, 30) # Z
when 0x1 then !bit?(@cpsr, 30) # !Z
when 0x2 then bit?(@cpsr, 29) # C
@ -44,6 +48,7 @@ class CPU
def tick : Nil
fill_pipeline
instr = @pipeline.shift
print_state instr
execute instr
end
@ -75,7 +80,7 @@ class CPU
if check_cond instr >> 28
hash = hash_instr instr
instr_type = Instr.from_hash hash
puts "Execute #{hex_str instr}: #{instr_type}"
log "Execute #{hex_str instr}: #{instr_type}"
case instr_type
when Instr::BRANCH
link = bit?(instr, 24)
@ -139,4 +144,11 @@ class CPU
puts "Skipping instruction, cond: #{hex_str instr >> 28}"
end
end
def print_state(instr : Word) : Nil
@r.each do |reg|
trace "#{hex_str reg, prefix: false} ", newline: false
end
trace "cpsr: #{hex_str @cpsr, prefix: false} | #{hex_str instr, prefix: false}"
end
end

View file

@ -1,5 +1,5 @@
def hex_str(n : UInt8 | UInt16 | UInt32 | UInt64) : String
"0x#{n.to_s(16).rjust(sizeof(typeof(n)) * 2, '0').upcase}"
def hex_str(n : UInt8 | UInt16 | UInt32 | UInt64, prefix = true) : String
(prefix ? "0x" : "") + "#{n.to_s(16).rjust(sizeof(typeof(n)) * 2, '0').upcase}"
end
macro bit?(value, bit)
@ -17,3 +17,23 @@ end
macro clear_bit(value, bit)
({{value}} & ~(1 << {{bit}}))
end
macro trace(value, newline = true)
{% if flag? :trace %}
{% if newline %}
puts {{value}}
{% else %}
print {{value}}
{% end %}
{% end %}
end
macro log(value, newline = true)
{% if flag?(:log) %}
{% if newline %}
puts {{value}}
{% else %}
print {{value}}
{% end %}
{% end %}
end