; Land Map 64 - generated terrain view with walking people and a status area (ca65). ; Built with: cl65 -t c64 -C c64-asm.cfg -u __EXEHDR__ (BASIC SYS line included). ; ; Multicolour bitmap palette and VIC bank layout: ; - multicolour bitmap mode with the Land Map 64 colour scheme ; background ($D021) brown (pixel %00) ; screen RAM high nibble terrain (pixel %01) grey, green, blue or black ; screen RAM low nibble terrain (pixel %10) ; colour RAM light red (pixel %11) people; constant, never flipped ; - bitmap buffer layout in separate VIC banks, selected through $DD00: ; buffer A: VIC bank 1, screen RAM $4000, bitmap $6000 ; buffer B: VIC bank 2, screen RAM $8000, bitmap $A000 (RAM under BASIC ROM) ; Both use $D018 = $08 (screen at bank+$0000, bitmap at bank+$2000), so a flip ; changes only the two bank bits in $DD00. ; ; During generation VIC bank 0 displays the message at $0400. In bitmap mode ; that text screen is hidden and holds the smoke test status mirror. .export smoke_ready, visible, keys_read, space_ready ; ---------------------------------------------------------------- hardware VIC_CTRL1 = $D011 ; bit 5 bitmap mode, bit 4 display on, bit 3 25 rows VIC_RASTER = $D012 VIC_CTRL2 = $D016 ; bit 4 multicolour, bit 3 40 columns VIC_MEMPTR = $D018 VIC_BORDER = $D020 VIC_BG0 = $D021 COLOR_RAM = $D800 CIA2_PRA = $DD00 ; bits 0-1: VIC bank, inverted (%10 = bank 1, %01 = bank 2) CPU_PORT = $01 TEXT_SCREEN = $0400 SCREEN_A = $4000 BITMAP_A = $6000 SCREEN_B = $8000 BITMAP_B = $A000 MAP_COLS_DRAWN = 39 ; column 39 is never visible in 38-column mode BUF_ROWS = 21 ; 20 map rows plus the partly visible row below BANK_TEXT_BITS = %00000011 ; VIC bank 0 ($0000-$3FFF) BANK_A_BITS = %00000010 ; VIC bank 1 ($4000-$7FFF) BANK_B_BITS = %00000001 ; VIC bank 2 ($8000-$BFFF) COL_BROWN = 9 COL_LIGHT_RED = 10 FLIP_RASTER = 251 ; below the visible area on NTSC and PAL ; Zero page: $FB-$FE are free for user programs. bmp = $FB ; pointer into the bitmap being drawn scr = $FD ; pointer into the screen RAM being drawn ; Screen codes (not PETSCII) for the status mirror: upper-case letters are 1-26. .repeat 26, I .charmap $41 + I, $01 + I .endrepeat .include "tiles.inc" .segment "CODE" start: lda #$36 ; BASIC ROM out, KERNAL and I/O in: the CPU can sta CPU_PORT ; read buffer B's bitmap at $A000 (needed from M2) jsr stop_kernal_irq jsr copy_font jsr install_raster_irq lda #0 sta last_seed_low sta last_seed_high jsr fill_color_ram jsr rebuild_map smoke_ready: ; first generated world is on screen nop ; Each frame, in vertical blank (before the first map line at raster 55): ; scroll one step if a cursor key is held, flipping buffers at a cell edge, ; then the people update (scroll.inc, people.inc). After that, with time to ; spare: read the keys, show the status and rebuild part of the hidden buffer. main_loop: jsr wait_frame jsr apply_view jsr people_vblank people_moved: ; check hook: people drawn for this frame jsr read_keys ; carry set: SPACE just pressed bcs new_world keys_read: ; check hook: scroll_dx and scroll_dy are set lda move_frame ; status every 4th frame (15 times a second) and #3 bne @rebuild jsr count_people_in_view jsr update_status @rebuild: jsr choose_rebuild_target jsr restore_hidden_people jsr rebuild_slices jmp main_loop new_world: jsr rebuild_map space_ready: ; regenerated map is on screen jmp main_loop ; ---------------------------------------------------------------- routines rebuild_map: jsr write_generating lda #COL_BROWN sta VIC_BORDER sta VIC_BG0 jsr wait_frame ; below the map: stop the split before jsr stop_raster_irq ; showing the message lda #BANK_TEXT_BITS jsr set_bank lda #$14 ; text screen at $0400, uppercase ROM font sta VIC_MEMPTR lda #$08 ; single colour, 40 columns sta VIC_CTRL2 lda #$1B ; text mode, 25 rows, y-scroll 3 sta VIC_CTRL1 jsr generate_world_from_timer jsr place_people lda #0 sta visible ; buffer A is shown first lda #1 sta hidden jsr reset_view ; view at 0,0; both buffers drawn jsr write_status_text jsr count_people_in_view jsr update_status jsr wait_frame ; switch banks and modes below the visible area lda #BANK_A_BITS jsr set_bank lda #MAP_D018 sta VIC_MEMPTR lda map_d016 sta VIC_CTRL2 lda map_d011 sta VIC_CTRL1 jsr start_raster_irq ; status area from this frame on ldx #STATUS_LENGTH-1 @ready: lda ready_text,x sta TEXT_SCREEN+80,x dex bpl @ready rts ; Wait until the raster is below the visible area (line FLIP_RASTER or later, ; up to the last line 262). First wait to leave that range, so each call returns ; once per frame. A range rather than one exact line: the KERNAL interrupt can ; run across line FLIP_RASTER, and an exact-line poll would then miss a frame. wait_frame: @leave: bit VIC_CTRL1 ; bit 7 = raster bit 8: lines 256-262 bmi @leave lda VIC_RASTER cmp #FLIP_RASTER bcs @leave @until: bit VIC_CTRL1 bmi @done lda VIC_RASTER cmp #FLIP_RASTER bcc @until @done: rts ; A = bank bits for $DD00. Keeps the other bits. set_bank: sta bank_bits lda CIA2_PRA and #%11111100 ora bank_bits sta CIA2_PRA rts ; Colour RAM is shared by both buffers: light red everywhere (pixel %11 = people). fill_color_ram: lda #COL_LIGHT_RED ldx #0 @loop: sta COLOR_RAM,x sta COLOR_RAM+$100,x sta COLOR_RAM+$200,x sta COLOR_RAM+$300,x inx bne @loop rts ; Prepare the text screen before switching the VIC to it. write_generating: lda #$20 ; screen code for space ldx #0 @clear: sta TEXT_SCREEN,x sta TEXT_SCREEN+$100,x sta TEXT_SCREEN+$200,x sta TEXT_SCREEN+$2E8,x ; covers up to $07E7 inx bne @clear ldx #0 @title: lda title_text,x beq @status sta TEXT_SCREEN,x inx bne @title @status: ldx #STATUS_LENGTH-1 @stat: lda generating_text,x sta TEXT_SCREEN+80,x ; row 2 dex bpl @stat rts .segment "RODATA" buffer_bank_bits: .byte BANK_A_BITS, BANK_B_BITS title_text: .byte "LAND MAP 64", 0 generating_text: .byte "GENERATING..." STATUS_LENGTH = * - generating_text ready_text: .byte "MAP READY " .assert * - ready_text = STATUS_LENGTH, error, "Status text length" .segment "BSS" bank_bits: .res 1 visible: .res 1 ; buffer on screen: 0 = A, 1 = B hidden: .res 1 ; the other buffer: visible EOR 1 .include "world.inc" .include "people.inc" .include "keys.inc" .include "status.inc" .include "scroll.inc"