1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-06-09 18:29:36 +00:00
kickc/src/test/ref/examples/c64/music/music_irq.asm
2024-01-02 19:21:16 +01:00

99 lines
3.1 KiB
NASM

// A simple SID music player using RASTER IRQ
/// @file
/// Commodore 64 Registers and Constants
/// @file
/// The MOS 6526 Complex Interface Adapter (CIA)
///
/// http://archive.6502.org/datasheets/mos_6526_cia_recreated.pdf
// Commodore 64 PRG executable file
.file [name="music_irq.prg", type="prg", segments="Program"]
.segmentdef Program [segments="Basic, Code, Data"]
.segmentdef Basic [start=$0801]
.segmentdef Code [start=$80d]
.segmentdef Data [startAfter="Code"]
.segment Basic
:BasicUpstart(main)
/// Value that disables all CIA interrupts when stored to the CIA Interrupt registers
.const CIA_INTERRUPT_CLEAR_ALL = $7f
/// VICII IRQ Status/Enable Raster
// @see #IRQ_ENABLE #IRQ_STATUS
/// 0 | RST| Reaching a certain raster line. The line is specified by writing
/// | | to register 0xd012 and bit 7 of $d011 and internally stored by
/// | | the VIC for the raster compare. The test for reaching the
/// | | interrupt raster line is done in cycle 0 of every line (for line
/// | | 0, in cycle 1).
.const IRQ_RASTER = 1
.const OFFSET_STRUCT_MOS6526_CIA_INTERRUPT = $d
.const OFFSET_STRUCT_MOS6569_VICII_CONTROL1 = $11
.const OFFSET_STRUCT_MOS6569_VICII_RASTER = $12
.const OFFSET_STRUCT_MOS6569_VICII_IRQ_ENABLE = $1a
.const OFFSET_STRUCT_MOS6569_VICII_BORDER_COLOR = $20
.const OFFSET_STRUCT_MOS6569_VICII_IRQ_STATUS = $19
/// The VIC-II MOS 6567/6569
.label VICII = $d000
/// The CIA#1: keyboard matrix, joystick #1/#2
.label CIA1 = $dc00
/// The vector used when the KERNAL serves IRQ interrupts
.label KERNEL_IRQ = $314
// Pointer to the music init routine
.label musicInit = MUSIC
// Pointer to the music play routine
.label musicPlay = MUSIC+3
.segment Code
// Raster IRQ Routine playing music
irq_play: {
// (VICII->BORDER_COLOR)++;
inc VICII+OFFSET_STRUCT_MOS6569_VICII_BORDER_COLOR
// (*musicPlay)()
// Play SID
jsr musicPlay
// VICII->IRQ_STATUS = IRQ_RASTER
// Acknowledge the IRQ
lda #IRQ_RASTER
sta VICII+OFFSET_STRUCT_MOS6569_VICII_IRQ_STATUS
// (VICII->BORDER_COLOR)--;
dec VICII+OFFSET_STRUCT_MOS6569_VICII_BORDER_COLOR
// }
jmp $ea31
}
// Setup Raster IRQ and initialize SID player
main: {
// asm
sei
// (*musicInit)()
jsr musicInit
// CIA1->INTERRUPT = CIA_INTERRUPT_CLEAR_ALL
// Disable CIA 1 Timer IRQ
lda #CIA_INTERRUPT_CLEAR_ALL
sta CIA1+OFFSET_STRUCT_MOS6526_CIA_INTERRUPT
// VICII->CONTROL1 &=$7f
// Set raster line to $fd
lda #$7f
and VICII+OFFSET_STRUCT_MOS6569_VICII_CONTROL1
sta VICII+OFFSET_STRUCT_MOS6569_VICII_CONTROL1
// VICII->RASTER = $fd
lda #$fd
sta VICII+OFFSET_STRUCT_MOS6569_VICII_RASTER
// VICII->IRQ_ENABLE = IRQ_RASTER
// Enable Raster Interrupt
lda #IRQ_RASTER
sta VICII+OFFSET_STRUCT_MOS6569_VICII_IRQ_ENABLE
// *KERNEL_IRQ = &irq_play
// Set the IRQ routine
lda #<irq_play
sta KERNEL_IRQ
lda #>irq_play
sta KERNEL_IRQ+1
// asm
cli
// }
rts
}
.segment Data
.pc = $1000 "MUSIC"
// SID tune at an absolute address
MUSIC:
.const music = LoadSid("toiletrensdyr.sid")
.fill music.size, music.getData(i)