coleco/sms: unified common.[ch]

This commit is contained in:
Steven Hugg committed 2026-09-05 15:46:33 +02:00
1 parent 03405df20b
commit 60963cbfd2
6 files changed
+88 -200

No files matched your search

+42 -3
View File
@@ -1,4 +1,3 @@
#include <stdlib.h>
#include <string.h>
#include <cv.h>
@@ -6,6 +5,11 @@
#include "common.h"
/* Unified common code for libcv platforms (coleco, msx-libcv, sms-sg1000-libcv,
sms-sms-libcv, sms-gg-libcv). Differences are selected by the CV_CV / CV_MSX /
CV_SMS defines (set by the platform) plus CV_MODE4 for SMS/GG mode-4 builds.
Keep this file identical across those preset directories. */
volatile uint_fast8_t vint_counter;
void vint_handler(void) {
@@ -22,16 +26,20 @@ byte reverse_bits(byte n) {
void flip_sprite_patterns(word dest, const byte* patterns, word len) {
word i;
for (i=0; i<len; i++) {
#if defined(CV_MODE4)
cvu_voutb(reverse_bits(*patterns++), dest++);
#else
cvu_voutb(reverse_bits(*patterns++), dest++ ^ 16); // swap left/right chars
#endif
}
}
void clrscr() {
cvu_vmemset(IMAGE, 0, COLS*ROWS);
cvu_vmemset(IMAGE, 0, ROWSTRIDE*ROWS);
}
word getimageaddr(byte x, byte y) {
return IMAGE + y*COLS + x;
return IMAGE + y*ROWSTRIDE + x;
}
byte getcharxy(byte x, byte y) {
@@ -48,6 +56,21 @@ void putstringxy(byte x, byte y, const char* string) {
}
}
#if defined(CV_MODE4)
// mode 4 aliases (same functions, shorter names)
byte getchar(byte x, byte y) {
return getcharxy(x, y);
}
void putchar(byte x, byte y, byte attr) {
putcharxy(x, y, attr);
}
void putstring(byte x, byte y, const char* string) {
putstringxy(x, y, string);
}
#endif
void delay(byte i) {
while (i--) {
wait_vsync();
@@ -103,11 +126,25 @@ __endasm;
void vdp_setup() {
cv_set_screen_active(false);
#if defined(CV_MODE4)
cv_set_screen_mode(CV_SCREENMODE_4_224);
cv_set_character_pattern_t(PATTERN | 0x3000);
cv_set_image_table(IMAGE | 0x400);
// cv_set_color_table(COLOR | 0xfff);
// cv_set_sprite_pattern_table(SPRITE_PATTERNS | 0x1800);
#else
cv_set_screen_mode(CV_SCREENMODE_STANDARD);
cv_set_image_table(IMAGE);
cv_set_character_pattern_t(PATTERN);
cv_set_color_table(COLOR);
cv_set_sprite_pattern_table(SPRITE_PATTERNS);
// load the default font and set a visible default color (white on black).
// the MSX startup code does this for us, but the ColecoVision and
// SG-1000 leave VRAM zeroed -- without this, putstringxy() output is
// invisible (blank pattern table) and the color table is transparent.
copy_default_character_set();
cvu_vmemset(COLOR, 0xf1, 32);
#endif
cv_set_sprite_attribute_table(SPRITES);
cv_set_sprite_big(true);
}
@@ -124,6 +161,7 @@ void set_shifted_pattern(const byte* src, word dest, byte shift) {
}
}
#if !defined(CV_MODE4)
void copy_default_character_set() {
#ifdef CV_MSX
static byte __at(0xf91f) CGPNT;
@@ -133,3 +171,4 @@ void copy_default_character_set() {
cvu_memtovmemcpy(PATTERN, (void *)(font_bitmap_0 - '0'*8), 256*8);
#endif
}
#endif
+40 -3
View File
@@ -2,27 +2,52 @@
#ifndef _CV_COMMON_H
#define _CV_COMMON_H
/* Unified common header for libcv platforms.
The build defines one of: CV_CV (ColecoVision), CV_MSX, CV_SMS (SG-1000/SMS/Game Gear).
CV_MODE4 is defined for SMS/GG mode-4 builds (2 bytes per tile, 32x28).
Keep this file identical across the coleco, msx-libcv, sms-sg1000-libcv,
sms-sms-libcv and sms-gg-libcv preset directories. */
#if defined(CV_MODE4)
/* VRAM map (mode 4)
0x0000 - 0x3fff character pattern table
0x3000 - 0x36ff image table
0x3c00 - 0x3fff sprite attribute table */
#define PATTERN ((const cv_vmemp)0x0000)
#define IMAGE ((const cv_vmemp)0x3000)
#define SPRITES ((const cv_vmemp)0x3c00)
#else
/* VRAM map
0x0000 - 0x17ff character pattern table
0x1800 - 0x1aff image table
0x2000 - 0x37ff color table
0x3800 - 0x3bff sprite pattern table
0x3c00 - 0x3fff sprite attribute table
*/
0x3c00 - 0x3fff sprite attribute table */
#define PATTERN ((const cv_vmemp)0x0000)
#define IMAGE ((const cv_vmemp)0x1800)
#define COLOR ((const cv_vmemp)0x2000)
#define SPRITE_PATTERNS ((const cv_vmemp)0x3800)
#define SPRITES ((const cv_vmemp)0x3c00)
#endif
#ifndef COLS
#define COLS 32
#endif
#ifndef ROWS
#if defined(CV_MODE4)
#define ROWS 28
#else
#define ROWS 24
#endif
#endif
/* bytes per row in the name table (mode 4 has a color byte per tile) */
#if defined(CV_MODE4)
#define ROWSTRIDE (COLS*2)
#else
#define ROWSTRIDE COLS
#endif
typedef unsigned char byte;
typedef signed char sbyte;
@@ -42,8 +67,12 @@ extern char font_bitmap_0[];
#define COLOR_FG(fg) (((fg)<<4))
#ifndef LOCHAR
#if defined(CV_MODE4)
#define LOCHAR 0x20
#else
#define LOCHAR 0x0
#endif
#endif
#define CHAR(ch) (ch-LOCHAR)
@@ -64,6 +93,12 @@ extern word getimageaddr(byte x, byte y);
extern byte getcharxy(byte x, byte y);
extern void putcharxy(byte x, byte y, byte attr);
extern void putstringxy(byte x, byte y, const char* string);
#if defined(CV_MODE4)
// mode 4 aliases (same functions, shorter names)
extern byte getchar(byte x, byte y);
extern void putchar(byte x, byte y, byte attr);
extern void putstring(byte x, byte y, const char* string);
#endif
extern void delay(byte i);
extern byte rndint(byte a, byte b);
@@ -77,6 +112,8 @@ extern word bcd_add(word a, word b);
extern void vdp_setup();
extern void set_shifted_pattern(const byte* src, word dest, byte shift);
#if !defined(CV_MODE4)
extern void copy_default_character_set();
#endif
#endif
-125
View File
@@ -1,125 +0,0 @@
#include <stdlib.h>
#include <string.h>
#include <cv.h>
#include <cvu.h>
#include "common.h"
volatile uint_fast8_t vint_counter;
void vint_handler(void) {
vint_counter++;
}
const unsigned char reverse_lookup[16] = {
0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe, 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf, };
byte reverse_bits(byte n) {
return (reverse_lookup[n&0b1111] << 4) | reverse_lookup[n>>4];
}
void flip_sprite_patterns(word dest, const byte* patterns, word len) {
word i;
for (i=0; i<len; i++) {
cvu_voutb(reverse_bits(*patterns++), dest++); // swap left/right chars
}
}
void clrscr() {
cvu_vmemset(IMAGE, 0, COLS*2*ROWS);
}
word getimageaddr(byte x, byte y) {
return IMAGE + y*COLS*2 + x;
}
byte getchar(byte x, byte y) {
return cvu_vinb(getimageaddr(x,y));
}
void putchar(byte x, byte y, byte attr) {
cvu_voutb(attr, getimageaddr(x,y));
}
void putstring(byte x, byte y, const char* string) {
while (*string) {
putchar(x++, y, CHAR(*string++));
}
}
void delay(byte i) {
while (i--) {
wait_vsync();
}
}
byte rndint(byte a, byte b) {
return ((byte)rand() % (b-a+1)) + a;
}
void memset_safe(void* _dest, char ch, word size) {
byte* dest = _dest;
while (size--) {
*dest++ = ch;
}
}
char in_rect(byte x, byte y, byte x0, byte y0, byte w, byte h) {
return ((byte)(x-x0) < w && (byte)(y-y0) < h); // unsigned
}
void draw_bcd_word(byte x, byte y, word bcd) {
byte j;
x += 3;
for (j=0; j<4; j++) {
putchar(x, y, CHAR('0'+(bcd&0xf)));
x--;
bcd >>= 4;
}
}
// add two 16-bit BCD values
word bcd_add(word a, word b) __naked {
a; b; // to avoid warning
__asm
push ix
ld ix,#0
add ix,sp
ld a,4 (ix)
add a, 6 (ix)
daa
ld c,a
ld a,5 (ix)
adc a, 7 (ix)
daa
ld b,a
ld l, c
ld h, b
pop ix
ret
__endasm;
}
void vdp_setup() {
cv_set_screen_active(false);
cv_set_screen_mode(CV_SCREENMODE_4_224);
cv_set_character_pattern_t(PATTERN | 0x3000);
cv_set_image_table(IMAGE | 0x400);
// cv_set_color_table(COLOR | 0xfff);
// cv_set_sprite_pattern_table(SPRITE_PATTERNS | 0x1800);
cv_set_sprite_attribute_table(SPRITES);
cv_set_sprite_big(true);
}
void set_shifted_pattern(const byte* src, word dest, byte shift) {
byte y;
for (y=0; y<8; y++) {
byte a = src[y+8];
byte b = src[y];
cvu_voutb(a>>shift, dest);
cvu_voutb(b>>shift | a<<(8-shift), dest+8);
cvu_voutb(b<<(8-shift), dest+16);
dest++;
}
}
+1
View File
@@ -0,0 +1 @@
../coleco/common.c
-68
View File
@@ -1,68 +0,0 @@
#ifndef _CV_COMMON_H
#define _CV_COMMON_H
/* VRAM map
0x0000 - 0x3fff character pattern table
0x3000 - 0x36ff image table
0x3c00 - 0x3fff sprite attribute table
*/
#define PATTERN ((const cv_vmemp)0x0000)
#define IMAGE ((const cv_vmemp)0x3000)
#define SPRITES ((const cv_vmemp)0x3c00)
#define COLS 32
#define ROWS 28
typedef unsigned char byte;
typedef signed char sbyte;
typedef unsigned short word;
#ifndef CV_SMS
uintptr_t __at(0x6a) font_bitmap_a;
uintptr_t __at(0x6c) font_bitmap_0;
#else
extern char font_bitmap_a[];
extern char font_bitmap_0[];
#endif
#define COLOR_FGBG(fg,bg) (((fg)<<4)|(bg))
#define COLOR_FG(fg) (((fg)<<4))
#define LOCHAR 0x20
#define HICHAR 0xff
#define CHAR(ch) (ch-LOCHAR)
#define wait_vsync() __asm__("halt")
extern volatile uint_fast8_t vint_counter;
extern void vint_handler(void);
extern byte reverse_bits(byte n);
extern void flip_sprite_patterns(word dest, const byte* patterns, word len);
extern char cursor_x;
extern char cursor_y;
extern void clrscr();
extern word getimageaddr(byte x, byte y);
extern byte getchar(byte x, byte y);
extern void putchar(byte x, byte y, byte attr);
extern void putstring(byte x, byte y, const char* string);
extern void delay(byte i);
extern byte rndint(byte a, byte b);
extern void memset_safe(void* _dest, char ch, word size);
extern char in_rect(byte x, byte y, byte x0, byte y0, byte w, byte h);
// print 4-digit BCD value
extern void draw_bcd_word(byte x, byte y, word bcd);
// add two 16-bit BCD values
extern word bcd_add(word a, word b);
extern void vdp_setup();
extern void set_shifted_pattern(const byte* src, word dest, byte shift);
#endif
+1
View File
@@ -0,0 +1 @@
../coleco/common.h
+1
View File
@@ -29,6 +29,7 @@ export function mockAudio() {
writeRegister() { }
writeRegisterSN() { }
writeRegisterAY() { }
readRegister() { return 0; }
}
class NullMasterChannel {
addChannel() { }
+3 -1
View File
@@ -410,6 +410,8 @@ export var PLATFORM_PARAMS = {
},
};
PLATFORM_PARAMS['sms-sms-libcv'] = PLATFORM_PARAMS['sms-sg1000-libcv'];
// SMS/GG mode-4 variants of the shared libcv common.[ch] (2 bytes per tile, 32x28)
PLATFORM_PARAMS['sms-sms-libcv'] = { ...PLATFORM_PARAMS['sms-sg1000-libcv'],
extra_preproc_args: ['-I', '.', '-D', 'CV_SMS', '-D', 'CV_MODE4'] };
PLATFORM_PARAMS['sms-gg-libcv'] = PLATFORM_PARAMS['sms-sms-libcv'];