Skip to content

Commit

Permalink
Petscii translation in stdio and conio
Browse files Browse the repository at this point in the history
  • Loading branch information
drmortalwombat committed Oct 12, 2021
1 parent da57ae0 commit 5372d49
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 263 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The goal is to implement the actual C standard and not some subset for performan

## Limits and Errors

After four weeks, the compiler has now matured significantly. There are still several open areas.
There are still several open areas, but most targets have been reached. The current Dhrystone performance is 35 iterations per second with byte code (12259) and 203 iterations with native code (13572 Bytes).

### Language

Expand Down Expand Up @@ -76,6 +76,19 @@ The compiler is command line driven, and creates an executable .prg file.

A list of source files can be provided.

## Console input and output

The C64 does not use ASCII it uses a derivative called PETSCII. There are two fonts, one with uppercase and one with uppercase and lowercase characters. It also used CR (13) as line terminator instead of LF (10). The stdio and conio libaries can perform translations.

The translation mode is selected in conio with the variable "giocharmap" and the function "iocharmap" which will also switch the font.

iocharmap(IOCHM_PETSCII_2);
printf("Hello World\");

Will switch to the lowercase PETSCII font and translate the strings while printing.

Input from the console will also be translated accordingly.

## Inline Assembler

Inline assembler can be embedded inside of any functions, regardles of their compilation target of byte code or native.
Expand Down
28 changes: 14 additions & 14 deletions include/c64/vic.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ enum VICColors
{
VCOL_BLACK,
VCOL_WHITE,
BCOL_RED,
BCOL_CYAN,
BCOL_PURPLE,
BCOL_GREEN,
BCOL_BLUE,
BCOL_YELLOW,
VCOL_RED,
VCOL_CYAN,
VCOL_PURPLE,
VCOL_GREEN,
VCOL_BLUE,
VCOL_YELLOW,

BCOL_ORANGE,
BCOL_BROWN,
BCOL_LT_RED,
BCOL_DARK_GREY,
BCOL_MED_GREY,
BCOL_LT_GREEN,
BCOL_LT_BLUE,
BCOL_LT_GREY
VCOL_ORANGE,
VCOL_BROWN,
VCOL_LT_RED,
VCOL_DARK_GREY,
VCOL_MED_GREY,
VCOL_LT_GREEN,
VCOL_LT_BLUE,
VCOL_LT_GREY
};

struct VIC
Expand Down
94 changes: 82 additions & 12 deletions include/conio.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,75 @@
#include "conio.h"

static IOCharMap giocharmap = IOCHM_ASCII;

void iocharmap(IOCharMap chmap)
{
giocharmap = chmap;
if (chmap == IOCHM_PETSCII_1)
putchar(128 + 14);
else if (chmap == IOCHM_PETSCII_2)
putchar(14);
}

__asm putpch
{
ldx giocharmap
cpx #IOCHM_ASCII
bcc w3

cmp #10
bne w1
lda #13
w1:
cpx #IOCHM_PETSCII_1
bcc w3

cmp #65
bcc w3
cmp #123
bcs w3
cmp #97
bcs w2
cmp #91
bcs w3
w2:
eor #$20
cpx #IOCHM_PETSCII_2
beq w3
and #$df
w3:
jmp 0xffd2
}

__asm getpch
{
jsr 0xffe4

ldx giocharmap
cpx #IOCHM_ASCII
bcc w3

cmp #13
bne w1
lda #10
w1:
cpx #IOCHM_PETSCII_1
bcc w3

cmp #65
bcc w3
cmp #123
bcs w3
cmp #97
bcs w2
cmp #91
bcs w3
w2:
eor #$20
w3:
}


int kbhit(void)
{
__asm
Expand All @@ -16,12 +86,12 @@ int getche(void)
__asm
{
L1:
jsr $ffe4
jsr getpch
cmp #0
beq L1

sta accu
jsr $ffd2
jsr putpch
lda #0
sta accu + 1
}
Expand All @@ -33,7 +103,7 @@ int getch(void)
__asm
{
L1:
jsr $ffe4
jsr getpch
cmp #0
beq L1

Expand All @@ -60,16 +130,17 @@ void clrscr(void)
}
}

void gotoxy(int x, int y)
void textcursor(bool show)
{
*(char *)0xcc = show ? 0 : 1;
}

void gotoxy(int cx, int cy)
{
__asm
{
ldy #y
lda (fp), y
tax
ldy #x
lda (fp), y
tay
ldy cy
ldx cx
clc
jsr $fff0
}
Expand All @@ -79,8 +150,7 @@ void textcolor(int c)
{
__asm
{
ldy #c
lda (fp), y
lda c
sta $0286
}
}
Expand Down
22 changes: 22 additions & 0 deletions include/conio.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
#ifndef CONIO_H
#define CONIO_H

enum IOCharMap
{
IOCHM_TRANSPARENT,
IOCHM_ASCII,
IOCHM_PETSCII_1,
IOCHM_PETSCII_2
};

extern IOCharMap giocharmap;

// Switch character map to transparent bypass, petscii font 1 or
// petscii font 2. Translation is performed for all reading and
// writing operations. The ascii mode will only translate the
// line end CR into an LF

void iocharmap(IOCharMap chmap);


int kbhit(void);

int getche(void);
Expand All @@ -19,6 +37,10 @@ int wherex(void);

int wherey(void);

// show or hide the text cursor

void textcursor(bool show);

#pragma compile("conio.c")

#endif
Expand Down
89 changes: 70 additions & 19 deletions include/stdio.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,77 @@
#include "stdio.h"
#include <stdlib.h>
#include "conio.h"
#include "stdlib.h"

void putchar(char c)
__asm putpch
{
__asm {
lda c
ldx giocharmap
cpx #IOCHM_ASCII
bcc w3

cmp #10
bne w1
lda #13
w1:
jsr 0xffd2
cpx #IOCHM_PETSCII_1
bcc w3

cmp #65
bcc w3
cmp #123
bcs w3
cmp #97
bcs w2
cmp #91
bcs w3
w2:
eor #$20
cpx #IOCHM_PETSCII_2
beq w3
and #$df
w3:
jmp 0xffd2
}

__asm getpch
{
jsr 0xffcf

ldx giocharmap
cpx #IOCHM_ASCII
bcc w3

cmp #13
bne w1
lda #10
w1:
cpx #IOCHM_PETSCII_1
bcc w3

cmp #65
bcc w3
cmp #123
bcs w3
cmp #97
bcs w2
cmp #91
bcs w3
w2:
eor #$20
w3:
}

void putchar(char c)
{
__asm {
lda c
jmp putpch
}
}

char getchar(void)
{
__asm {
jsr 0xffcf
jsr getpch
sta accu
lda #0
sta accu + 1
Expand All @@ -25,21 +81,16 @@ char getchar(void)
void puts(const char * str)
{
__asm {
loop:
ldy #0
lda (str), y
beq done
loop:
cmp #10
bne w1
lda #13
w1:
jsr 0xffd2

jsr putpch

inc str
bne next
bne loop
inc str + 1
next:
ldy #0
lda (str), y
bne loop
done:
}
Expand All @@ -49,14 +100,14 @@ char * gets(char * str)
{
__asm {
loop:
jsr 0xffcf
jsr getpch
ldy #0
cmp #13
cmp #10
beq done
sta (str), y
inc str
bne loop
inc srt + 1
inc str + 1
bne loop
done:
lda #0
Expand Down
1 change: 1 addition & 0 deletions include/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stdlib.h>


void putchar(char c);

char getchar(void);
Expand Down
Loading

0 comments on commit 5372d49

Please sign in to comment.