diff --git a/compiler/res/prog8lib/cx16/emudbg.p8 b/compiler/res/prog8lib/cx16/emudbg.p8 index fe26f3e29..c8b55e625 100644 --- a/compiler/res/prog8lib/cx16/emudbg.p8 +++ b/compiler/res/prog8lib/cx16/emudbg.p8 @@ -24,39 +24,34 @@ emudbg { sub is_emulator() -> bool { ; Test for emulator presence. - ; It is recommended to only use the other debug routines if this returns true. + ; It is recommended to only use the debug registers if this returns true. return EMU_EMU_DETECT1=='1' and EMU_EMU_DETECT2=='6' } - asmsub console_write(str isoString @R0) clobbers(Y) { + sub console_write(str isoString) { ; note: make sure the text is in Iso encoding. - %asm {{ - ldy #0 -- lda (cx16.r0),y - beq + - sta p8_EMU_CPUCLK_U - iny - bne - -+ - }} + if is_emulator() { + cx16.r1 = isoString + while @(cx16.r1) { + EMU_CPUCLK_U = @(cx16.r1) + cx16.r1++ + } + } } - asmsub console_chrout(ubyte char @A) { + sub console_chrout(ubyte char) { ; note: make sure the character is in Iso encoding. - %asm {{ - sta p8_EMU_CPUCLK_U - }} + if is_emulator() + EMU_CPUCLK_U = char } - asmsub console_value1(ubyte value @A) { - %asm {{ - sta p8_EMU_CPUCLK_M - }} + sub console_value1(ubyte value) { + if is_emulator() + EMU_CPUCLK_M = value } - asmsub console_value2(ubyte value @A) { - %asm {{ - sta p8_EMU_CPUCLK_H - }} + sub console_value2(ubyte value) { + if is_emulator() + EMU_CPUCLK_H = value } } diff --git a/compiler/res/prog8lib/virtual/emudbg.p8 b/compiler/res/prog8lib/virtual/emudbg.p8 new file mode 100644 index 000000000..e98cc7603 --- /dev/null +++ b/compiler/res/prog8lib/virtual/emudbg.p8 @@ -0,0 +1,34 @@ +; Emulator debug interface. (reflecting the Commander X16 emudbg library module) +; Docs: https://github.com/X16Community/x16-emulator#emulator-io-registers + +%import textio + +emudbg { + + sub is_emulator() -> bool { + ; Test for emulator presence. + return true ; VM is always 'emulator' + } + + sub console_write(str message) { + txt.print("[EMUDBG: ") + txt.print(message) + txt.chrout(']') + } + + sub console_chrout(ubyte char) { + txt.chrout(char) + } + + sub console_value1(ubyte value) { + txt.print("[EMUDBG debug 1: ") + txt.print_uwhex(value, true) + txt.print("]\n") + } + + sub console_value2(ubyte value) { + txt.print("[EMUDBG debug 2: ") + txt.print_uwhex(value, true) + txt.print("]\n") + } +} diff --git a/examples/test.p8 b/examples/test.p8 index 80a127da5..9803acd92 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,12 +1,21 @@ %zeropage basicsafe %import textio +%import emudbg main { sub start() { - str name="irmen" - if name=="." { - cx16.r0++ - } + txt.print("is emulator? ") + txt.print_ub(emudbg.is_emulator()) txt.nl() + + emudbg.console_write(iso:"Hello from emulator.\n") + emudbg.console_chrout(iso:'1') + emudbg.console_chrout(iso:'2') + emudbg.console_chrout(iso:'3') + emudbg.console_chrout(iso:'\n') + emudbg.console_value1(123) + emudbg.console_chrout(iso:'\n') + emudbg.console_value2(42) + emudbg.console_chrout(iso:'\n') } }