Skip to content

Commit bfa13e6

Browse files
author
ExpandingMan
committed
Started working on assembler.
1 parent c4fcf60 commit bfa13e6

File tree

5 files changed

+121
-14
lines changed

5 files changed

+121
-14
lines changed

src/Sim6502.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ include("boilerplate.jl")
1919
include("instructions.jl")
2020
include("chipset.jl")
2121
include("opcodes.jl")
22+
include("assembler.jl")
2223

2324
end # module

src/boilerplate.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,14 @@ end
131131

132132

133133
macro opdef(opname::Symbol, defblock::Expr)
134-
@capture(defblock, begin defs__ end)
135-
defs = [tuple(ex.args...) for ex defs]
136-
funcdefs = [opdef(d[1], opname, d[2]) for d defs]
137-
dictries = [opdict(d[2], d[3], d[4]) for d defs]
138-
assemblies = [assemblydict(opname, d[1], d[2]) for d defs]
134+
if @capture(defblock, begin defs__ end)
135+
defs = [ex.args for ex defs]
136+
funcdefs = [opdef(d[1], opname, d[2]) for d defs]
137+
dictries = [opdict(d[2], d[3], d[4]) for d defs]
138+
assemblies = [assemblydict(opname, d[1], d[2]) for d defs]
139+
else
140+
throw(AssertionError("Improper op definition for $opname."))
141+
end
139142
esc(quote
140143
$(funcdefs...)
141144
$(dictries...)

src/instructions.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,11 +477,11 @@ export clc!, cld!, cli!, clv!, sec!, sed!, sei!
477477
#===================================================================================================
478478
<system functions>
479479
===================================================================================================#
480-
# TODO this will require some special handling
481480
brk!(c::CPU) = status!(c, :B, true)
482481

483482
nop!(c::CPU) = ()
484483

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

src/opcodes.jl

Lines changed: 98 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,18 @@ end
3232
# this doesn't do the sleeping
3333
function op!(cs::Chipset)
3434
exe!, nbytes, ncycles = OPCODES[deref(Π(cs.cpu.PC), cs.ram)]
35+
exe!(cs)
3536
cs.cpu.PC += nbytes
3637
cs.clock += ncycles
37-
exe!(cs), nbytes, ncycles
38+
nbytes, ncycles
39+
end
40+
41+
# TODO eventually this should employ timing
42+
function run!(cs::Chipset)
43+
while !cs.cpu.flags.B
44+
op!(cs)
45+
println(cs)
46+
end
3847
end
3948

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

6170
# format is
62-
# mode, opcode, nbytes, ncycles
71+
# mode, opcode, nbytes, ncycles, (increment)
72+
#
73+
# the last arg is optional and tells it whether or not to increment the instruction pointer
6374

6475
# we treat "Accumulator" mode as "Implicit"
6576

@@ -298,10 +309,92 @@ end
298309
AbsoluteX, 0x7e, 3, 7
299310
end
300311

301-
# TODO haven't decided how to do this yet!
312+
# note that these get 0's because they don't increment the instruction pointer
302313
@opdef jmp! begin
303-
Immediate, 0x4c, 3, 3
304-
Absolute, 0x6c, 3, 5
314+
Immediate, 0x4c, 0, 3
315+
Absolute, 0x6c, 0, 5
316+
end
317+
318+
@opdef jsr! begin
319+
Immediate, 0x20, 0, 6
320+
end
321+
322+
# TODO not sure if implicit works with cpu and ram arguments
323+
@opdef rts! begin
324+
Implicit, 0x60, 0, 6
325+
end
326+
327+
# TODO check if instruction pointer is handled correctly in branches
328+
@opdef bcc! begin
329+
Immediate, 0x90, 2, 2
330+
end
331+
332+
@opdef bcs! begin
333+
Immediate, 0xb0, 2, 2
334+
end
335+
336+
@opdef beq! begin
337+
Immediate, 0xf0, 2, 2
338+
end
339+
340+
@opdef bmi! begin
341+
Immediate, 0x30, 2, 2
342+
end
343+
344+
@opdef bne! begin
345+
Immediate, 0xd0, 2, 2
346+
end
347+
348+
@opdef bpl! begin
349+
Immediate, 0x10, 2, 2
350+
end
351+
352+
@opdef bvc! begin
353+
Immediate, 0x50, 2, 2
354+
end
355+
356+
@opdef bvs! begin
357+
Immediate, 0x70, 2, 2
358+
end
359+
360+
@opdef clc! begin
361+
Implicit, 0x18, 1, 2
362+
end
363+
364+
@opdef cld! begin
365+
Implicit, 0xd8, 1, 2
366+
end
367+
368+
@opdef cli! begin
369+
Implicit, 0x58, 1, 2
370+
end
371+
372+
@opdef clv! begin
373+
Implicit, 0xb8, 1, 2
374+
end
375+
376+
@opdef sec! begin
377+
Implicit, 0x38, 1, 2
378+
end
379+
380+
@opdef sed! begin
381+
Implicit, 0xf8, 1, 2
382+
end
383+
384+
@opdef sei! begin
385+
Implicit, 0x78, 1, 2
386+
end
387+
388+
@opdef brk! begin
389+
Implicit, 0x00, 1, 7
390+
end
391+
392+
@opdef nop! begin
393+
Implicit, 0xea, 1, 2
394+
end
395+
396+
@opdef rti! begin
397+
Implicit, 0x40, 1, 6
305398
end
306399
#===================================================================================================
307400
</opcodes>

test/opcodes.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,17 @@ function makechipset()
2828
cs
2929
end
3030

31-
# TODO investigate allocs!!!
31+
32+
function showstates()
33+
cs = makechipset()
34+
35+
@p op!(cs)
36+
@p op!(cs)
37+
@p op!(cs)
38+
39+
cs
40+
end
41+
3242

3343
function runbenches()
3444
ref = @benchmarkable begin
@@ -47,4 +57,4 @@ function runbenches()
4757
end
4858

4959

50-
ref, b = runbenches()
60+
# ref, b = runbenches()

0 commit comments

Comments
 (0)