Skip to content

Commit

Permalink
added koala parser and example for #7 and #9
Browse files Browse the repository at this point in the history
  • Loading branch information
Shazz committed Jan 30, 2021
1 parent 853b67d commit 910b635
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 16 deletions.
72 changes: 72 additions & 0 deletions examples/i_love_koalas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# if shazzam not installed
import sys
sys.path.append(".")

from reloading import reloading
from shazzam.Segment import SegmentType
from shazzam.py64gen import *
from shazzam.py64gen import RegisterX as x, RegisterY as y, RegisterACC as a
from shazzam.macros.aliases import color, vic
from shazzam.drivers.assemblers.CC65 import CC65
import shazzam.plugins.plugins as p
import shazzam.macros.vic as v


# define your cross assembler
assembler = CC65("cc65", "third_party/cc65/bin/cl65")
prefs = assembler.get_code_format()
set_prefs(default_code_segment=assembler.get_code_segment(),
code_format=prefs.code,
comments_format=prefs.comments,
directive_prefix=prefs.directive)

@reloading
def code():

# define here or anywhere, doesn't matter, your variables
kla = p.read_kla('resources/panda.kla')
vic_bank = 0x4000
screen_mem = 0x2000
bitmap_mem = 0x0000
char_mem = 0x0000 # not used, 0 is fine

# CC65 generates basic header, no macro needed just to define the CODE segment
with segment(0x0801, assembler.get_code_segment()) as s:

v.setup_vic_bank(vic_bank) # set vic bank

d018 = v.generate_d018(char_mem, bitmap_mem, screen_mem)
lda(imm(d018))
sta(at(vic.mem_setup)) # set vic memory setup

lda(imm(0b00111000)) # set bitmap mode
sta(at(vic.scr_ctrl))

lda(imm(0b11011000)) # set multicolor mode on
sta(at(vic.scr_ctrl2))

lda(imm(kla.bg_color)) # set border and window color to picture background color
sta(at(vic.border_col))
sta(at(vic.bck_col))

label("loop")
jmp(at("loop"))

# set bitmap data at vic_bank + bimap
with segment(vic_bank, "bitmap", segment_type=SegmentType.BITMAP) as s:
incbin(kla.bitmap)

with segment(vic_bank+screen_mem, "screen_ram", segment_type=SegmentType.SCREEN_MEM) as s:
incbin(kla.scrmem)

with segment(0xd800, "color_ram", segment_type=SegmentType.REGISTERS) as s:
incbin(kla.colorram)

# generate listing
gen_code(format_code=prefs, gen_listing=True)

# finally assemble segments to PRG using cross assembler then crunch it!
assemble_prg(assembler, start_address=0x0801)

if __name__ == "__main__":
generate(code, "i_love_koalas")
7 changes: 2 additions & 5 deletions shazzam/Segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,10 @@ def __init__(self, start_adr: int, name: str,
if directive_prefix is None:
directive_prefix = g._DIRECTIVE_PREFIX

self.logger.info(
f"Prefs: {code_format} / {comments_format} / {directive_prefix}")
self.logger.debug("Prefs: {code_format} / {comments_format} / {directive_prefix}")

self.show_address = True if CodeFormat.ADDRESS in code_format else False
self.show_bytecode = True if CodeFormat.BYTECODE in code_format else False
self.logger.error(f"format: {code_format}")
self.show_cycles = True if CodeFormat.CYCLES in code_format else False
self.use_uppercase = True if CodeFormat.UPPERCASE in code_format else False
self.use_hex = True if CodeFormat.USE_HEX in code_format else False
Expand All @@ -120,8 +118,7 @@ def change_format(self):
comments_format = g._COMMENTS_FORMAT
directive_prefix = g._DIRECTIVE_PREFIX

self.logger.debug(
f"Reset Prefs: {code_format} / {comments_format} / {directive_prefix}")
self.logger.debug(f"Reset Prefs: {code_format} / {comments_format} / {directive_prefix}")

self.show_address = True if CodeFormat.ADDRESS in code_format else False
self.show_bytecode = True if CodeFormat.BYTECODE in code_format else False
Expand Down
20 changes: 9 additions & 11 deletions shazzam/macros/vic.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def clear_screen(clear_byte: int, screen: int, use_ptr: bool):
# Setup VIC bacnk
# Usind dd00 or dd02
# ------------------------------------------------------------------------------------------
def setup_vic_bank(bank_start, use_dd02):
def setup_vic_bank(bank_start, use_dd02: bool = False):

def generate_dd00(bank_start):
if bank_start == 0:
Expand Down Expand Up @@ -75,15 +75,17 @@ def generate_full_dd02(bank_start):
return reg

if use_dd02:
dd02 = utils.generate_full_dd02(bank_start)
dd02 = generate_full_dd02(bank_start)

logger.info("bank", bank_start, BITMAP_ADDR, SCREEN_MEM_ADDR, "dd02", dd02, "d018", utils.generate_d018(CHAR_MEM_ADDR, BITMAP_ADDR, SCREEN_MEM_ADDR), d018, "mixed", (dd02 | d018))
logger.info(f"Bank start: ${bank_start:04X} using dd02 register: %{dd02:08b}")
lda(imm(dd02))
sta(at(dd02))
sta(at(0xdd02))
else:
dd00 = utils.generate_dd00(bank_start)
dd00 = generate_dd00(bank_start)

logger.info(f"Bank start: ${bank_start:04X} using dd00 register: %{dd00:08b}")
lda(imm(dd00))
sta(at(dd00))
sta(at(0xdd00))

# ------------------------------------------------------------------------------------------
# Generate d018 register
Expand Down Expand Up @@ -121,12 +123,8 @@ def generate_d018(charmem, bitmap, screenmem):
0x3800: 0b1110,
0x3C00: 0b1111
}

logger.debug(charmem, mapping_charmem[parseInt(charmem)])
logger.debug(bitmap, mapping_bitmap[bitmap])
logger.debug(screenmem, mapping_screenmem[screenmem])

reg = ((mapping_charmem[charmem] << 1) | (mapping_bitmap[bitmap] << 1) | (mapping_screenmem[screenmem] << 4))
logger.info(f"VIC memory setup: %{reg:08b} for char mem at ${charmem:04X}, bitmap at ${bitmap:04X} and screen mem at ${screenmem:04X}")
return reg

# ------------------------------------------------------------------------
Expand Down
33 changes: 33 additions & 0 deletions shazzam/plugins/plugins.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
from shazzam.defs import *

# ---------------------------------------------------------------------
# Koala file parser
#
# KLA file format information
# bytes 0000-0001 = Load address (2 bytes)
# bytes 0002-1F42 = bitmap data (8000 bytes)
# bytes 1F43-232A = screen memory data (1000 bytes)
# bytes 232B-2711 = color ram data (1000 bytes)
# bytes 2712 = background color (1 byte)

# ---------------------------------------------------------------------
def read_kla(filename: str) -> Alias:

def readWord(buf, offs):
return buf[offs] + (buf[offs+1] << 8)

with open(filename, "rb") as f:
buf = f.read()
address = readWord(buf, 0)

bitmap = buf[2:8002]
scrmem = buf[8002:9002]
colorram = buf[9002:10002]
bg_color = buf[10002]

return Alias({
"address": address,
"bg_color": bg_color,
"bitmap": bitmap,
"scrmem": scrmem,
"colorram": colorram,
})

# ---------------------------------------------------------------------
# SPD Sprites file parser
#
Expand Down

0 comments on commit 910b635

Please sign in to comment.