Skip to content

Commit

Permalink
A little more work on pointers and a few more instructions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ExpandingMan committed May 7, 2017
1 parent 95ffbdf commit 22adee9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/Sim6502.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module Sim6502

import Base.fetch
import Base: +, -

include("utils.jl")
include("cpu.jl")
include("memory.jl")
Expand Down
1 change: 0 additions & 1 deletion src/cpu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ end
export CPU


import Base.fetch
fetch(c::CPU, reg::Symbol) = getfield(c, reg)
store!(c::CPU, reg::Symbol, val::Integer) = setfield!(c, reg, val)
export fetch, store!
Expand Down
31 changes: 29 additions & 2 deletions src/instructions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ checkZflag!(c::CPU, val::UInt8) = val == 0x00 ? status!(c, :Z, true) : status!(c
#===================================================================================================
<load, store instructions>
===================================================================================================#
#-----------------------LDA, LDX, LDY----------------------------------------------
# template for ld instructions in immediate mode
function ld!(c::CPU, register::Symbol, val::UInt8)
v = setfield!(c, register, val)
function ld!(c::CPU, reg::Symbol, val::UInt8)
v = store!(c, reg, val)
checkNflag!(c, v)
checkZflag!(c, v)
counter!(c, 0x02)
Expand All @@ -21,7 +22,33 @@ lda!(c::CPU, val::UInt8) = ld!(c, :A, val)
ldx!(c::CPU, val::UInt8) = ld!(c, :X, val)
ldy!(c::CPU, val::UInt8) = ld!(c, :Y, val)

# template for ld instructions in zero page or absolute mode
ld!(c::CPU, m::Memory, reg::Symbol, ptr:) = ld!(c, reg, ptr m)

lda!(c::CPU, m::Memory, ptr:) = ld!(c, m, :A, ptr)
ldx!(c::CPU, m::Memory, ptr:) = ld!(c, m, :X, ptr)
ldy!(c::CPU, m::Memory, ptr:) = ld!(c, m, :Y, ptr)

# zero page,X,Y and absolute,X,Y; note this also works with A, even though that's not a real instruction!
ld!(c::CPU, m::Memory, reg::Symbol, ptr:, idx::Symbol) = ld!(c, m, reg, (ptr + fetch(c,idx)) m)

lda!(c::CPU, m::Memory, ptr:, idx::Symbol) = ld!(c, m, :A, ptr, idx)
ldx!(c::CPU, m::Memory, ptr:, idx::Symbol) = ld!(c, m, :X, ptr, idx)
ldy!(c::CPU, m::Memory, ptr:, idx::Symbol) = ld!(c, m, :Y, ptr, idx)

export ld!, lda!, ldx!, ldy!


#---------------------STA, STX, STY------------------------------------------------
# template for st in zero page or absolute mode
st!(c::CPU, m::Memory, reg::Symbol, ptr:) = store!(m, ptr, fetch(c, reg))

# zero page or absolute mode
sta!(c::CPU, m::Memory, ptr:) = st!(c, m, :A, ptr)
stx!(c::CPU, m::Memory, ptr:) = st!(c, m, :X, ptr)
sty!(c::CPU, m::Memory, ptr:) = st!(c, m, :Y, ptr)

export st!, sta!, stx!, sty!
#===================================================================================================
</load, store instructions>
===================================================================================================#
Expand Down
28 changes: 20 additions & 8 deletions src/memory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,32 @@ reset!(m::Memory) = (m.v .= zeros(UInt8, length(m.v)))
#===================================================================================================
<pointers>
===================================================================================================#
abstract type AbstractMPtr end
abstract type AbstractΠ end # uses capital Π

type MPtr{T<:Unsigned} <: AbstractMPtr
type Π{T<:Unsigned} <: AbstractΠ
addr::T

MPtr{T}(addr::Unsigned) where T<:Unsigned = new(addr)
Π{T}(addr::Unsigned) where T<:Unsigned = new(addr)
end
export MPtr
export Π

dereference{T}(ptr::MPtr{T}, m::Memory) = m.v[ptr.addr+one(T)]
deref(ptr::MPtr, m::Memory) = dereference(ptr, m)
(ptr::MPtr, m::Memory) = dereference(ptr, m)
const Π8 = Π{UInt8} # zero page pointer
const Π16 = Π{UInt16} # standard 6502 memory pointer
export Π8, Π16

export dereference, deref,
dereference{T}(ptr:{T}, m::Memory) = m.v[ptr.addr+one(T)]
deref(ptr:, m::Memory) = dereference(ptr, m)
(ptr:, m::Memory) = dereference(ptr, m) # this symbol is \mapsto

store!(m::Memory, ptr:{T}, val::UInt8) = (m.v[ptr.addr+one(T)] = val)

(+){T}(ptr1:{T}, ptr2:{T}) = Π{T}(ptr1.addr + ptr2.addr)
(-){T}(ptr1:{T}, ptr2:{T}) = Π{T}(ptr1.addr - ptr2.addr)

(+){T}(ptr:{T}, val::Unsigned) = Π{T}(ptr.addr + convert(T, val))
(-){T}(ptr:{T}, val::Unsigned) = Π{T}(ptr.addr - convert(T, val))

export dereference, deref, , store!, +, -
#===================================================================================================
</pointers>
===================================================================================================#
Expand Down

0 comments on commit 22adee9

Please sign in to comment.