Last active
June 30, 2024 10:52
-
-
Save adiee5/22bbc588cf5c5329ef57d6dd48a5fae8 to your computer and use it in GitHub Desktop.
An example of a queue in prog8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Simple library, that implements a queue | |
; The code bellow expects the user to define a `queue_cfg` block alongside some values. See queue_demo.p8 for more info | |
queue{ | |
uword @zp fi | |
uword @zp fo | |
ubyte state=0 | |
const ubyte initialised=1 | |
const ubyte danger=2 | |
; initialize the queue | |
sub init(){ | |
fi=queue_cfg.startaddr | |
fo=queue_cfg.startaddr | |
state|=initialised | |
} | |
; current length of the queue | |
sub length()->uword{ | |
if (state&initialised==0)return 0 | |
if fi<fo or state&danger!=0{ | |
return (queue_cfg.endaddr+1-queue_cfg.startaddr)-(fo-fi) | |
} | |
return fi-fo | |
} | |
; add a value to queue | |
sub push(ubyte b){ | |
if(state&initialised==0)return | |
if state&danger!=0{ | |
queue_cfg.on_runtimeerr() | |
return | |
} | |
@(fi)=b | |
fi++ | |
if (fi==queue_cfg.endaddr+1) fi=queue_cfg.startaddr | |
if (fi==fo) state|=danger | |
return | |
} | |
; remove a value from the queue | |
sub pop()->ubyte{ | |
if(state&initialised==0) or (fo==fi and state&danger==0){ | |
queue_cfg.on_runtimeerr() | |
return 0 ; it's entirely possible, that it won't reach this | |
} | |
result = @(fo) | |
fo++ | |
if(fo==queue_cfg.endaddr+1) fo=queue_cfg.startaddr | |
state&=~danger | |
return result | |
} | |
; io functions (will only work on cbm targets) | |
sub print(){ | |
repeat{ | |
if(queue.length()==0)break | |
void pop() | |
if(result==0)break | |
cbm.CHROUT(result) | |
} | |
} | |
sub input(){ | |
repeat{ | |
result=cbm.CHRIN() | |
if(result==$0d)break | |
push(result) | |
} | |
;push(0) ;idk really if this should be explicit or not | |
} | |
ubyte result | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; a queue demo for cx16 | |
%import queue | |
%import textio | |
%zeropage basicsafe | |
%option no_sysinit | |
main{ | |
sub start(){ | |
queue.init() | |
queue.push('h') | |
queue.push('e') | |
queue.push('l') | |
queue.push('l') | |
queue.push('o') | |
queue.push('\n') | |
queue.push(0) | |
txt.print("len=") | |
txt.print_uw(queue.length()) | |
txt.nl() | |
queue.print() | |
queue.input() | |
queue.push(0) | |
txt.print("len=") | |
txt.print_uw(queue.length()) | |
txt.print("\nhey ") | |
queue.print() | |
} | |
} | |
; A static configuration of our Queue. It's supposed to be editable by the developer that uses the library. | |
queue_cfg{ | |
; start and end addresses of our queue. Can be defined as either variables or as constants, depending on what will be more apropriate in our usecase. | |
const uword startaddr = $0400 | |
const uword endaddr = $07ff | |
; This code gets executed whenever a runtime error occurs. | |
inline asmsub on_runtimeerr(){ | |
%asm{{ | |
brk | |
}} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment