Skip to content

Commit

Permalink
* optimized binary size
Browse files Browse the repository at this point in the history
 - 768 bytes for ASM (was 1280)
 - 1380 for C (was 1725)
* fixed: unreachable code warnings for C code
  • Loading branch information
zoul0813 committed Jan 12, 2025
1 parent f2097e8 commit bf4f42f
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 132 deletions.
130 changes: 69 additions & 61 deletions src/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ DEFC IO_TEXT_CURS_TIME = VID_IO_BANKED_ADDR + 0x6 ; Blink time, in frames, for
DEFC IO_TEXT_CURS_CHAR = VID_IO_BANKED_ADDR + 0x7 ; Blink time, in frames, for the cursor
DEFC IO_TEXT_CURS_COLOR = VID_IO_BANKED_ADDR + 0x8 ; Blink time, in frames, for the cursor

DEFC CHARCODE_OFFSET = 0x000
DEFC COLORCODE_OFFSET = 0x100
DEFC SINECOSINE_OFFSET = 0x200

; first step is to create a table with sine + cosine values
; The addition is performed on a proportionate basis
; the table is changed on every frame
Expand All @@ -51,14 +55,61 @@ _start:
ld h, 0x10
ld bc, 0x0000 ; Map VRAM on Page 2: 0x8000
MAP()
;

; generate charcode table
;
; Generate the Charcode Table
; repeat each byte in charcodes 16 times, generating a 256-byte table
;
ld de, charcodes + 0x0F ; last charcode
ld hl, TABLES + CHARCODE_OFFSET + 0xFF ; end of table
next_charcode:
ld a, (de)
ld bc, 15 ; count down from
charcode_loop:
ld (hl), a
dec l ; table_ptr--
dec c ; j--
jp p, charcode_loop
ld a, e
and a, 0x0F
dec a
jp m, charcode_done
dec e ; i--
jp next_charcode
charcode_done:
;

; generate colorcode table
;
; Generate the Colorcode Table
;
ld de, colorcodes + 0x0F
ld hl, TABLES + COLORCODE_OFFSET + 0xFF
next_colorcode:
ld a, (de)
ld bc, 15
colorcode_loop:
ld (hl), a
dec l ; -- table_ptr--
dec c ; j--
jp p, colorcode_loop
ld a, e
and a, 0x0F
dec a
jp m, colorcode_done
dec e ; i--
jp next_colorcode
colorcode_done:

loop:
ld a, ROWS+COLUMNS
hl_addr:
ld hl, tbl_sin ; self modifiying
bc_addr:
ld bc, tbl_cos ; self modifying
ld de, sinecosine
ld de, TABLES + SINECOSINE_OFFSET
sincos_loop:
push af
ld a, (bc)
Expand All @@ -81,7 +132,7 @@ sincos_loop:
ld c, ROWS-1 ; for(row = ROWS; row > 0; row--)
ld hl, VRAM_TEXT
; sinecosine[COLUMNS + row] will be preclaculated and decremented at eaach row iteraiton
ld de, sinecosine + COLUMNS + ROWS
ld de, TABLES + SINECOSINE_OFFSET + COLUMNS + ROWS
row_loop:
; sinecosine is aligned on 256 bytes for sure
dec e
Expand All @@ -94,15 +145,15 @@ col_loop:
; The code below doesn't modify DE, nor C !

push hl
ld h, sinecosine >> 8 ; HL = &sinecosine
ld h, TABLES + SINECOSINE_OFFSET >> 8 ; HL = &sinecosine
ld l, e ; HL = &sinecosine[COLUMN]
ld a, (hl) ; A = sinecosine[COLUMN]
add d ; A += offset

; lsa a
; Since charcode is aligned on 256, its lower byte is 0x00 for sure.
; So HL + A is HA
ld h, charcode >> 8
ld h, TABLES + CHARCODE_OFFSET >> 8
ld l, a
ld b, (hl) ; B = charcode[offset]

Expand Down Expand Up @@ -175,61 +226,18 @@ _end:
EXIT()
;

ALIGN 0x10
charcodes:
DB 254,249,250,46
DB 254,249,250,46
DB 254,249,250,46
DB 254,249,250,46

; codecodes:
; DB 178,177,176,219 ; set 1
; DB 254, 249, 250, 46 ; set 2
; DB 220, 223, 254, 219 ; set 3

;
ALIGN 0x100
charcode:
DS 16,254 ;
DS 16,249 ; number of bytes used will determine
DS 16,250 ; the thickness of the layers pattern
DS 16,46 ;

DS 16,254 ;
DS 16,249 ; number of bytes used will determine
DS 16,250 ; the thickness of the layers pattern
DS 16,46 ;

DS 16,254 ;
DS 16,249 ; number of bytes used will determine
DS 16,250 ; the thickness of the layers pattern
DS 16,46 ;

DS 16,254 ;
DS 16,249 ; number of bytes used will determine
DS 16,250 ; the thickness of the layers pattern
DS 16,46 ;

;

;
ALIGN 0x100 ; here the code is aligned
; so that the LB adress is at $00
colorcode:
DS 16, 1 ; dark blue
DS 16, 9 ; purple
DS 16, 5 ; magenta
DS 16, 13 ; cyan

DS 16,0 ; black
DS 16,1 ; dark blue
DS 16,9 ; purple
DS 16,8 ; dark grey

DS 16, 8 ; dark grey
DS 16, 9 ; purple
DS 16, 5 ; magenta
DS 16, 7 ; light grey

DS 16, 0 ; black
DS 16, 3 ; teal
DS 16, 11 ; orange
DS 16, 15 ; white
;
colorcodes:
DB 1,9,5,13
DB 0,1,9,8
DB 8,9,5,7
DB 0,3,11,15

;
ALIGN 0x100 ; "sin 256" table is comprised of 512 bytes
Expand Down Expand Up @@ -276,6 +284,6 @@ tbl_cos:
DB 59,55,51,47,42,36,31,25,20,15,11,7,4,1,0,0
;

;
ALIGN 0x100
sinecosine: ; max value in sinecosine is 63+63=126 (x 2 =252)
; this should already be aligned to 0x100
; ALIGN 0x100
TABLES:
122 changes: 51 additions & 71 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,24 @@
#define COLUMNS 80
#define ROWS 40


/** tables */
uint8_t charcode[256] = {
254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,
249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,
250,250,250,250,250,250,250,250,250,250,250,250,250,250,250,250,
46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,

254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,
249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,
250,250,250,250,250,250,250,250,250,250,250,250,250,250,250,250,
46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,

254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,
249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,
250,250,250,250,250,250,250,250,250,250,250,250,250,250,250,250,
46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,

254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,
249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,
250,250,250,250,250,250,250,250,250,250,250,250,250,250,250,250,
46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,46 ,
uint8_t charcodes[] = {
254,249,250,46,
254,249,250,46,
254,249,250,46,
254,249,250,46,
};

uint8_t colorcode[256] = {
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,

00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,

8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,

00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
uint8_t colorcodes[] = {
1,9,5,13,
0,1,9,8,
8,9,5,7,
0,3,11,15,
};

/** tables */
uint8_t charcode[256];
uint8_t colorcode[256];

uint8_t sin[256] = {
32,28,24,20,16,13,10,7,5,3,1,0,0,0,0,1,
2,4,6,9,11,15,18,22,26,30,33,37,41,45,48,52,
Expand Down Expand Up @@ -97,8 +69,6 @@ uint8_t cos[256] = {

uint8_t sinecosine[ROWS+COLUMNS];

uint8_t value = 0; // valued used for each column

uint8_t mmu_page_current;
const __sfr __banked __at(0xF0) mmu_page0_ro;
__sfr __at(0xF2) mmu_page2;
Expand All @@ -110,63 +80,73 @@ __sfr __banked __at(0x9d) vid_ctrl_status;
static inline void text_map_vram(void)
{
mmu_page_current = mmu_page0_ro;
// __asm__("di");
__asm__("di");
mmu_page2 = VID_MEM_PHYS_ADDR_START >> 14;
}

static inline void text_demap_vram(void)
{
// __asm__("ei");
__asm__("ei");
mmu_page2 = mmu_page_current;
}

/* start */
static zos_err_t err;
static uint8_t sin_offset = 0;
static uint8_t cos_offset = 0;

int main(void) {
zos_err_t err;
void main(void) {
uint8_t *ptr = NULL;
uint8_t i = 0, j = 0, k = 0, v = 0;

// disable cursor
zvb_peri_text_curs_time = 0;
err = ioctl(DEV_STDOUT, CMD_CLEAR_SCREEN, (void *)NULL);
if(err != ERR_SUCCESS) exit(err);

text_map_vram();
// generate charcode table
ptr = &charcode[255];
for(i = 0; i < 16; i++) {
k = charcodes[i];
for(j = 0; j < 16; j++) {
*ptr = k;
ptr--;
}
}

uint8_t col = 0, row = 0;
uint8_t i = 0; // i = column, j = row
// // generate colorcode table
ptr = &colorcode[255];
for(i = 0; i < 16; i++) {
k = colorcodes[i];
for(j = 0; j < 16; j++) {
*ptr = k;
ptr--;
}
}

text_map_vram();

uint8_t loops = 0;
while(1) {
// start
for(i = 0; i < ROWS+COLUMNS; i++) {
uint8_t c = cos[cos_offset];
uint8_t s = sin[sin_offset];
sinecosine[i] = c + s;
j = cos[cos_offset];
k = sin[sin_offset];
sinecosine[i] = j + k;

sin_offset++;
cos_offset--;
}

// plot
for(col = 0; col < COLUMNS; col++) {
uint8_t c = sinecosine[col];
uint8_t *r = &sinecosine[COLUMNS];
for(row = 0; row < ROWS; row++) {
uint8_t offset = c + *r;
SCR_TEXT[row][col] = charcode[offset];
SCR_COLOR[row][col] = colorcode[offset];
r++;
for(i = 0; i < COLUMNS; i++) {
k = sinecosine[i];
ptr = &sinecosine[COLUMNS];
for(j = 0; j < ROWS; j++) {
v = k + *ptr;
SCR_TEXT[j][i] = charcode[v];
SCR_COLOR[j][i] = colorcode[v];
ptr++;
}
}
loops++;
}

// unreachable
text_demap_vram();

err = ioctl(DEV_STDOUT, CMD_RESET_SCREEN, NULL);

return err;
}

0 comments on commit bf4f42f

Please sign in to comment.