From c22a67f95f278819bc9e92d1b6c8b3a98c28cb34 Mon Sep 17 00:00:00 2001 From: Arne Schmitz Date: Tue, 11 Mar 2025 16:56:06 +0100 Subject: [PATCH 1/6] basic version detection and and implement bsplot for PET --- include/conio.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 1 deletion(-) diff --git a/include/conio.c b/include/conio.c index ec4b02d..8664b5d 100644 --- a/include/conio.c +++ b/include/conio.c @@ -131,6 +131,97 @@ __asm bsinit lda #147 jmp $ffd2 } +#elif defined(__CBMPET__) +#define bsout 0xffd2 +#define bsin 0xffe4 + +char detect_basic() +{ + static const char basicV4[] = p"*** commodore basic 4.0 ***"; + static const char basicV2[] = p"### commodore basic ###"; + static const char basicV1[] = p"*** commodore basic ***"; + static const char editor[] = {0x4C,0x4B,0xE0,0x4C,0xA7,0xE0,0x4C,0x16,0xE1}; + + if( memcmp( (const void*)0xdea4, basicV4, 27 ) == 0 ) { + if( memcmp( (const void*)0xe000, editor, 8 ) == 0) + return 48; + else + return 44; + } else if( memcmp( (const void*)0xe180, basicV1, 23 ) == 1 ) { + return 1; + } else if( memcmp( (const void*)0xe1c4, basicV2, 23 ) == 1 ) { + return 2; + } else { + printf("unknown basic rom!\n"); + } + return 0; +} + +static char basic_version = 0; + +char get_basic_version() +{ + if(basic_version == 0) + basic_version = detect_basic(); + return basic_version; +} + +void petplot(char cx, char cy) +{ + switch( get_basic_version() ) + { + case 48: + __asm + { + /* BASIC4 80-col */ + ldx cx + ldx cy + stx $e2 + sty $e0 + jsr 0xe05f + } + break; + case 44: + __asm + { + /* BASIC4 40-col */ + ldx cx + ldx cy + stx $c6 + sty $d8 + jsr 0xe07f + } + break; + case 2: + __asm + { + /* BASIC2 */ + ldx cx + ldx cy + stx $c6 + sty $d8 + jsr 0xe25d + } + break; + default: + __asm + { + /* BASIC1 */ + ldx cx + ldx cy + stx $e2 + sty $f5 + jsr 0xe5db + } + break; + } +} + +__asm bsinit +{ + /* no equivalent on PET */ +} +#define bsget 0xffcf #else #define bsout 0xffd2 #define bsin 0xffe4 @@ -341,13 +432,17 @@ void textcursor(bool show) void gotoxy(char cx, char cy) { +#ifdef __CBMPET__ + petplot(cx, cy); +#else __asm { ldx cy ldy cx clc jsr bsplot - } + } +#endif } void textcolor(char c) From 4f100266c3892e3c025faa00305ec87828db34f5 Mon Sep 17 00:00:00 2001 From: Arne Schmitz Date: Tue, 11 Mar 2025 17:46:36 +0100 Subject: [PATCH 2/6] fix ldy --- include/conio.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/conio.c b/include/conio.c index 8664b5d..3db4c62 100644 --- a/include/conio.c +++ b/include/conio.c @@ -175,7 +175,7 @@ void petplot(char cx, char cy) { /* BASIC4 80-col */ ldx cx - ldx cy + ldy cy stx $e2 sty $e0 jsr 0xe05f @@ -186,7 +186,7 @@ void petplot(char cx, char cy) { /* BASIC4 40-col */ ldx cx - ldx cy + ldy cy stx $c6 sty $d8 jsr 0xe07f @@ -197,7 +197,7 @@ void petplot(char cx, char cy) { /* BASIC2 */ ldx cx - ldx cy + ldy cy stx $c6 sty $d8 jsr 0xe25d @@ -208,7 +208,7 @@ void petplot(char cx, char cy) { /* BASIC1 */ ldx cx - ldx cy + ldy cy stx $e2 sty $f5 jsr 0xe5db From ee68f2575ad8919c173add5d320c9b11566af5fc Mon Sep 17 00:00:00 2001 From: Arne Schmitz Date: Fri, 14 Mar 2025 17:28:50 +0100 Subject: [PATCH 3/6] better implementation, inspired by cc65 --- include/conio.c | 116 ++++++++++++++---------------------------------- 1 file changed, 33 insertions(+), 83 deletions(-) diff --git a/include/conio.c b/include/conio.c index 3db4c62..406e302 100644 --- a/include/conio.c +++ b/include/conio.c @@ -134,89 +134,9 @@ __asm bsinit #elif defined(__CBMPET__) #define bsout 0xffd2 #define bsin 0xffe4 - -char detect_basic() -{ - static const char basicV4[] = p"*** commodore basic 4.0 ***"; - static const char basicV2[] = p"### commodore basic ###"; - static const char basicV1[] = p"*** commodore basic ***"; - static const char editor[] = {0x4C,0x4B,0xE0,0x4C,0xA7,0xE0,0x4C,0x16,0xE1}; - - if( memcmp( (const void*)0xdea4, basicV4, 27 ) == 0 ) { - if( memcmp( (const void*)0xe000, editor, 8 ) == 0) - return 48; - else - return 44; - } else if( memcmp( (const void*)0xe180, basicV1, 23 ) == 1 ) { - return 1; - } else if( memcmp( (const void*)0xe1c4, basicV2, 23 ) == 1 ) { - return 2; - } else { - printf("unknown basic rom!\n"); - } - return 0; +__asm bsplot{ + /* no equivalent on PET */ } - -static char basic_version = 0; - -char get_basic_version() -{ - if(basic_version == 0) - basic_version = detect_basic(); - return basic_version; -} - -void petplot(char cx, char cy) -{ - switch( get_basic_version() ) - { - case 48: - __asm - { - /* BASIC4 80-col */ - ldx cx - ldy cy - stx $e2 - sty $e0 - jsr 0xe05f - } - break; - case 44: - __asm - { - /* BASIC4 40-col */ - ldx cx - ldy cy - stx $c6 - sty $d8 - jsr 0xe07f - } - break; - case 2: - __asm - { - /* BASIC2 */ - ldx cx - ldy cy - stx $c6 - sty $d8 - jsr 0xe25d - } - break; - default: - __asm - { - /* BASIC1 */ - ldx cx - ldy cy - stx $e2 - sty $f5 - jsr 0xe5db - } - break; - } -} - __asm bsinit { /* no equivalent on PET */ @@ -433,7 +353,37 @@ void textcursor(bool show) void gotoxy(char cx, char cy) { #ifdef __CBMPET__ - petplot(cx, cy); +#define CURS_X $c6 +#define CURS_Y $d8 +#define SCREEN_PTR $c4 +#define SCR_LINELEN $d5 + + static const char ScrLo[] = { 0x00, 0x28, 0x50, 0x78, 0xA0, 0xC8, 0xF0, 0x18, + 0x40, 0x68, 0x90, 0xB8, 0xE0, 0x08, 0x30, 0x58, + 0x80, 0xA8, 0xD0, 0xF8, 0x20, 0x48, 0x70, 0x98, + 0xC0 }; + + static const char ScrHi[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, + 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, + 0x03 }; + __asm { + lda cx + sta CURS_X + lda cy + sta CURS_Y + ldy CURS_Y + lda ScrLo,y + sta SCREEN_PTR + lda ScrHi,y + ldy SCR_LINELEN + cpy #40+1 + bcc col80 + asl SCREEN_PTR /* 80 column mode */ + rol + col80: ora #$80 /* Screen at $8000 */ + sta SCREEN_PTR+1 + } #else __asm { From 2c37e150a96fb285a50cc39f8b3ba0b238bbe69e Mon Sep 17 00:00:00 2001 From: Arne Schmitz Date: Fri, 14 Mar 2025 17:29:29 +0100 Subject: [PATCH 4/6] comment --- include/conio.c | 1 + 1 file changed, 1 insertion(+) diff --git a/include/conio.c b/include/conio.c index 406e302..4a6014d 100644 --- a/include/conio.c +++ b/include/conio.c @@ -353,6 +353,7 @@ void textcursor(bool show) void gotoxy(char cx, char cy) { #ifdef __CBMPET__ + /* This is inspired by the cc65 source code from pet/cputc.s */ #define CURS_X $c6 #define CURS_Y $d8 #define SCREEN_PTR $c4 From ab2ca897ce3d0b4084fccebe400566a080d6e32c Mon Sep 17 00:00:00 2001 From: Arne Schmitz Date: Sun, 16 Mar 2025 17:04:25 +0100 Subject: [PATCH 5/6] pure asm implementation of bsplot --- include/conio.c | 108 +++++++++++++++++++++++++++++++----------------- 1 file changed, 71 insertions(+), 37 deletions(-) diff --git a/include/conio.c b/include/conio.c index 4a6014d..790be25 100644 --- a/include/conio.c +++ b/include/conio.c @@ -134,8 +134,77 @@ __asm bsinit #elif defined(__CBMPET__) #define bsout 0xffd2 #define bsin 0xffe4 -__asm bsplot{ - /* no equivalent on PET */ +/* This is inspired by the cc65 source code from pet/cputc.s */ +#define CURS_X $c6 +#define CURS_Y $d8 +#define SCREEN_PTR $c4 +#define SCR_LINELEN $d5 +__asm bsplot +{ + sty CURS_X + stx CURS_Y +plot: ldy CURS_Y + lda ScrLo,y + sta SCREEN_PTR + lda ScrHi,y + ldy SCR_LINELEN + cpy #40+1 + bcc col80 + asl SCREEN_PTR /* 80 column mode */ + rol +col80: ora #$80 /* Screen at $8000 */ + sta SCREEN_PTR+1 + rts +ScrLo: byt 0x00 + byt 0x28 + byt 0x50 + byt 0x78 + byt 0xA0 + byt 0xC8 + byt 0xF0 + byt 0x18 + byt 0x40 + byt 0x68 + byt 0x90 + byt 0xB8 + byt 0xE0 + byt 0x08 + byt 0x30 + byt 0x58 + byt 0x80 + byt 0xA8 + byt 0xD0 + byt 0xF8 + byt 0x20 + byt 0x48 + byt 0x70 + byt 0x98 + byt 0xC0 +ScrHi: byt 0x00 + byt 0x00 + byt 0x00 + byt 0x00 + byt 0x00 + byt 0x00 + byt 0x00 + byt 0x01 + byt 0x01 + byt 0x01 + byt 0x01 + byt 0x01 + byt 0x01 + byt 0x02 + byt 0x02 + byt 0x02 + byt 0x02 + byt 0x02 + byt 0x02 + byt 0x02 + byt 0x03 + byt 0x03 + byt 0x03 + byt 0x03 + byt 0x03 } __asm bsinit { @@ -352,40 +421,6 @@ void textcursor(bool show) void gotoxy(char cx, char cy) { -#ifdef __CBMPET__ - /* This is inspired by the cc65 source code from pet/cputc.s */ -#define CURS_X $c6 -#define CURS_Y $d8 -#define SCREEN_PTR $c4 -#define SCR_LINELEN $d5 - - static const char ScrLo[] = { 0x00, 0x28, 0x50, 0x78, 0xA0, 0xC8, 0xF0, 0x18, - 0x40, 0x68, 0x90, 0xB8, 0xE0, 0x08, 0x30, 0x58, - 0x80, 0xA8, 0xD0, 0xF8, 0x20, 0x48, 0x70, 0x98, - 0xC0 }; - - static const char ScrHi[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, - 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, - 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, - 0x03 }; - __asm { - lda cx - sta CURS_X - lda cy - sta CURS_Y - ldy CURS_Y - lda ScrLo,y - sta SCREEN_PTR - lda ScrHi,y - ldy SCR_LINELEN - cpy #40+1 - bcc col80 - asl SCREEN_PTR /* 80 column mode */ - rol - col80: ora #$80 /* Screen at $8000 */ - sta SCREEN_PTR+1 - } -#else __asm { ldx cy @@ -393,7 +428,6 @@ void gotoxy(char cx, char cy) clc jsr bsplot } -#endif } void textcolor(char c) From 33cabb487b48cddf5804d7387fbde94673ecb987 Mon Sep 17 00:00:00 2001 From: Arne Schmitz Date: Sun, 23 Mar 2025 13:15:09 +0100 Subject: [PATCH 6/6] pure C implementation --- include/conio.c | 89 +++++++++++-------------------------------------- 1 file changed, 20 insertions(+), 69 deletions(-) diff --git a/include/conio.c b/include/conio.c index 790be25..e34e932 100644 --- a/include/conio.c +++ b/include/conio.c @@ -134,77 +134,9 @@ __asm bsinit #elif defined(__CBMPET__) #define bsout 0xffd2 #define bsin 0xffe4 -/* This is inspired by the cc65 source code from pet/cputc.s */ -#define CURS_X $c6 -#define CURS_Y $d8 -#define SCREEN_PTR $c4 -#define SCR_LINELEN $d5 __asm bsplot { - sty CURS_X - stx CURS_Y -plot: ldy CURS_Y - lda ScrLo,y - sta SCREEN_PTR - lda ScrHi,y - ldy SCR_LINELEN - cpy #40+1 - bcc col80 - asl SCREEN_PTR /* 80 column mode */ - rol -col80: ora #$80 /* Screen at $8000 */ - sta SCREEN_PTR+1 - rts -ScrLo: byt 0x00 - byt 0x28 - byt 0x50 - byt 0x78 - byt 0xA0 - byt 0xC8 - byt 0xF0 - byt 0x18 - byt 0x40 - byt 0x68 - byt 0x90 - byt 0xB8 - byt 0xE0 - byt 0x08 - byt 0x30 - byt 0x58 - byt 0x80 - byt 0xA8 - byt 0xD0 - byt 0xF8 - byt 0x20 - byt 0x48 - byt 0x70 - byt 0x98 - byt 0xC0 -ScrHi: byt 0x00 - byt 0x00 - byt 0x00 - byt 0x00 - byt 0x00 - byt 0x00 - byt 0x00 - byt 0x01 - byt 0x01 - byt 0x01 - byt 0x01 - byt 0x01 - byt 0x01 - byt 0x02 - byt 0x02 - byt 0x02 - byt 0x02 - byt 0x02 - byt 0x02 - byt 0x02 - byt 0x03 - byt 0x03 - byt 0x03 - byt 0x03 - byt 0x03 + /* no equivalent on PET */ } __asm bsinit { @@ -421,6 +353,24 @@ void textcursor(bool show) void gotoxy(char cx, char cy) { +#if defined(__CBMPET__) +#define CURS_X 0xc6 +#define CURS_Y 0xd8 +#define SCREEN_PTR 0xc4 +#define SCR_LINELEN 0xd5 + + __assume(cy < 25); + + *(volatile char *)CURS_X = cx; + *(volatile char *)CURS_Y = cy; + + if (*(volatile char *)SCR_LINELEN > 40) + cy <<= 1; + + const unsigned off = cy * 40; + + * (volatile unsigned *)SCREEN_PTR = off + 0x8000; +#else __asm { ldx cy @@ -428,6 +378,7 @@ void gotoxy(char cx, char cy) clc jsr bsplot } +#endif } void textcolor(char c)