Skip to content

Commit

Permalink
Started working on assembler.
Browse files Browse the repository at this point in the history
  • Loading branch information
ExpandingMan committed May 31, 2017
1 parent c4fcf60 commit bfa13e6
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/Sim6502.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ include("boilerplate.jl")
include("instructions.jl")
include("chipset.jl")
include("opcodes.jl")
include("assembler.jl")

end # module
13 changes: 8 additions & 5 deletions src/boilerplate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,14 @@ end


macro opdef(opname::Symbol, defblock::Expr)
@capture(defblock, begin defs__ end)
defs = [tuple(ex.args...) for ex defs]
funcdefs = [opdef(d[1], opname, d[2]) for d defs]
dictries = [opdict(d[2], d[3], d[4]) for d defs]
assemblies = [assemblydict(opname, d[1], d[2]) for d defs]
if @capture(defblock, begin defs__ end)
defs = [ex.args for ex defs]
funcdefs = [opdef(d[1], opname, d[2]) for d defs]
dictries = [opdict(d[2], d[3], d[4]) for d defs]
assemblies = [assemblydict(opname, d[1], d[2]) for d defs]
else
throw(AssertionError("Improper op definition for $opname."))
end
esc(quote
$(funcdefs...)
$(dictries...)
Expand Down
4 changes: 2 additions & 2 deletions src/instructions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,11 @@ export clc!, cld!, cli!, clv!, sec!, sed!, sei!
#===================================================================================================
<system functions>
===================================================================================================#
# TODO this will require some special handling
brk!(c::CPU) = status!(c, :B, true)

nop!(c::CPU) = ()

# TODO this is likely fucked up
# __NOTE!!__ that PC is also incremented the normal way afterwards from reading the instruction
function rti!(c::CPU, m::Memory)
ptr = stackpointer(c) + 0x01 # are you sure about this?
Expand All @@ -490,7 +490,7 @@ function rti!(c::CPU, m::Memory)
pc_small = deref(ptr, m)
ptr = ptr + 0x01
pc_big = deref(ptr, m)
c.PC = 0x0100*pc_big + pc_small
c.PC = bincat(pc_small, pc_big)
c.SP += 0x03 # not completely confident in this either
end

Expand Down
103 changes: 98 additions & 5 deletions src/opcodes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,18 @@ end
# this doesn't do the sleeping
function op!(cs::Chipset)
exe!, nbytes, ncycles = OPCODES[deref(Π(cs.cpu.PC), cs.ram)]
exe!(cs)
cs.cpu.PC += nbytes
cs.clock += ncycles
exe!(cs), nbytes, ncycles
nbytes, ncycles
end

# TODO eventually this should employ timing
function run!(cs::Chipset)
while !cs.cpu.flags.B
op!(cs)
println(cs)
end
end

export op!, tick!
Expand All @@ -59,7 +68,9 @@ const OPCODES = Vector{Tuple{OpFunc,Int,Int}}(N_OPCODES)
const ASSEMBLY_DICT = Dict{Tuple{String,Symbol},UInt8}(); sizehint!(ASSEMBLY_DICT, N_OPCODES)

# format is
# mode, opcode, nbytes, ncycles
# mode, opcode, nbytes, ncycles, (increment)
#
# the last arg is optional and tells it whether or not to increment the instruction pointer

# we treat "Accumulator" mode as "Implicit"

Expand Down Expand Up @@ -298,10 +309,92 @@ end
AbsoluteX, 0x7e, 3, 7
end

# TODO haven't decided how to do this yet!
# note that these get 0's because they don't increment the instruction pointer
@opdef jmp! begin
Immediate, 0x4c, 3, 3
Absolute, 0x6c, 3, 5
Immediate, 0x4c, 0, 3
Absolute, 0x6c, 0, 5
end

@opdef jsr! begin
Immediate, 0x20, 0, 6
end

# TODO not sure if implicit works with cpu and ram arguments
@opdef rts! begin
Implicit, 0x60, 0, 6
end

# TODO check if instruction pointer is handled correctly in branches
@opdef bcc! begin
Immediate, 0x90, 2, 2
end

@opdef bcs! begin
Immediate, 0xb0, 2, 2
end

@opdef beq! begin
Immediate, 0xf0, 2, 2
end

@opdef bmi! begin
Immediate, 0x30, 2, 2
end

@opdef bne! begin
Immediate, 0xd0, 2, 2
end

@opdef bpl! begin
Immediate, 0x10, 2, 2
end

@opdef bvc! begin
Immediate, 0x50, 2, 2
end

@opdef bvs! begin
Immediate, 0x70, 2, 2
end

@opdef clc! begin
Implicit, 0x18, 1, 2
end

@opdef cld! begin
Implicit, 0xd8, 1, 2
end

@opdef cli! begin
Implicit, 0x58, 1, 2
end

@opdef clv! begin
Implicit, 0xb8, 1, 2
end

@opdef sec! begin
Implicit, 0x38, 1, 2
end

@opdef sed! begin
Implicit, 0xf8, 1, 2
end

@opdef sei! begin
Implicit, 0x78, 1, 2
end

@opdef brk! begin
Implicit, 0x00, 1, 7
end

@opdef nop! begin
Implicit, 0xea, 1, 2
end

@opdef rti! begin
Implicit, 0x40, 1, 6
end
#===================================================================================================
</opcodes>
Expand Down
14 changes: 12 additions & 2 deletions test/opcodes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ function makechipset()
cs
end

# TODO investigate allocs!!!

function showstates()
cs = makechipset()

@p op!(cs)
@p op!(cs)
@p op!(cs)

cs
end


function runbenches()
ref = @benchmarkable begin
Expand All @@ -47,4 +57,4 @@ function runbenches()
end


ref, b = runbenches()
# ref, b = runbenches()

0 comments on commit bfa13e6

Please sign in to comment.