Skip to content

Instantly share code, notes, and snippets.

@intoinside
Created June 4, 2023 16:17
Show Gist options
  • Save intoinside/ff0d07e86685408c9b48ea8888e5cb4c to your computer and use it in GitHub Desktop.
Save intoinside/ff0d07e86685408c9b48ea8888e5cb4c to your computer and use it in GitHub Desktop.
Short example for threading with c64 (KickAssembler)
*= $0801 "Basic Upstart"
BasicUpstart2(Entry)
* = * "Entry"
Entry: {
sei
lda #<context_switch
ldx #>context_switch
sta $0314
stx $0315
// initialize threads
ldx #0
stx thread_num
// main thread is automatically setup by first irq
// we only need to setup further threads
// split stack
tsx
txa
tay
sec
sbc #$20
tax
txs
// push thread data
// program counter, status register, a, x, y
lda #>thread2
pha
lda #<thread2
pha
lda #0
pha
pha
pha
pha
// save stack pointer
tsx
txa
sta thread_data+1
// restore old stack pointer
tya
tax
txs
cli
// go to main thread
jmp thread1
}
* = * "context_switch"
context_switch: {
//--------------------------------------------------------------------------
// if you're not using the kernal don't forget to save/restore A, X, Y *using the stack*!
// save stack pointer
ldy thread_num
tsx
txa
sta thread_data,y
// next thread, wraparound
iny
cpy #num_threads
bne nowrap
ldy #0
nowrap:
sty thread_num
// restore thread
lda thread_data,y
tax
txs
jmp $ea31
}
//--------------------------------------------------------------------------
// thread 1 switches the border color
thread1: {
inc $d020
ldy #$01
jsr wait2
jmp thread1
}
// thread 2 displays a text message
thread2: {
lda #<msg1
ldy #>msg1
jsr $ab1e
inc $d021
ldy #0
jsr wait2
jmp thread2
}
// delay a shitload of cycles
wait2: {
!:
ldx #0
dex
bne *-1
dey
bne !-
rts
}
//--------------------------------------------------------------------------
msg1: .text "HELLO, HERE IS THREAD 2!"
.byte $00
.label num_threads = 2
.label thread_num = $fd // current thread number
thread_data:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment