Updated core to 1.8

This commit is contained in:
rdanbrook 2018-02-03 21:58:45 -05:00
parent 47ff87724a
commit 3db43eb00c
16 changed files with 481 additions and 161 deletions

View file

@ -1,13 +1,13 @@
CC = cc
LDFLAGS =
FLAGS = -Icore \
-Wall -g \
-DLSB_FIRST \
#-O3 -fomit-frame-pointer -ffast-math
-Wall -g \
-DLSB_FIRST \
#-O3 -fomit-frame-pointer -ffast-math
LIBS = -lm -lz $(shell pkg-config --libs glfw3 epoxy ao)
LIBS = -lm $(shell pkg-config --libs glfw3 epoxy ao)
OBJ = obj/z80.o \
OBJ = obj/z80.o \
obj/sms.o \
obj/pio.o \
obj/memz80.o \
@ -18,9 +18,10 @@ OBJ = obj/z80.o \
obj/error.o
OBJ += obj/state.o \
obj/loadrom.o
obj/loadrom.o \
obj/hash.o
OBJ += obj/sound.o \
OBJ += obj/sound.o \
obj/sn76489.o \
obj/emu2413.o \
obj/ym2413.o \

189
core/hash.c Normal file
View file

@ -0,0 +1,189 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "hash.h"
void sha1_transform(uint32_t state[5], const uint8_t buffer[64]) {
// Hash a single 512-bit block. This is the core of the algorithm.
uint32_t a, b, c, d, e;
typedef union {
uint8_t c[64];
uint32_t l[16];
} CHAR64LONG16;
CHAR64LONG16* block;
block = (CHAR64LONG16*)buffer;
a = state[0];
b = state[1];
c = state[2];
d = state[3];
e = state[4];
R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
state[4] += e;
a = b = c = d = e = 0;
}
void sha1_init(sha1_context_t* context) {
// Initialize new context
context->state[0] = 0x67452301;
context->state[1] = 0xEFCDAB89;
context->state[2] = 0x98BADCFE;
context->state[3] = 0x10325476;
context->state[4] = 0xC3D2E1F0;
context->count[0] = context->count[1] = 0;
}
void sha1_update(sha1_context_t* context, const uint8_t* data, const size_t len) {
// Run the data through this
size_t i, j;
j = (context->count[0] >> 3) & 63;
if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++;
context->count[1] += (len >> 29);
if ((j + len) > 63) {
memcpy(&context->buffer[j], data, (i = 64-j));
sha1_transform(context->state, context->buffer);
for ( ; i + 63 < len; i += 64) {
sha1_transform(context->state, data + i);
}
j = 0;
}
else { i = 0; }
memcpy(&context->buffer[j], &data[i], len - i);
}
void sha1_final(sha1_context_t* context, uint8_t digest[SHA1_DIGEST_SIZE]) {
// Add padding and return the message digest
uint32_t i;
uint8_t finalcount[8];
for (i = 0; i < 8; i++) {
finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)]
>> ((3-(i & 3)) * 8) ) & 255);
}
sha1_update(context, (uint8_t *)"\200", 1);
while ((context->count[0] & 504) != 448) {
sha1_update(context, (uint8_t *)"\0", 1);
}
sha1_update(context, finalcount, 8);
for (i = 0; i < SHA1_DIGEST_SIZE; i++) {
digest[i] = (uint8_t)
((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
}
i = 0;
memset(context->buffer, 0, 64);
memset(context->state, 0, 20);
memset(context->count, 0, 8);
memset(finalcount, 0, 8);
}
const char* sha1(const void *buf, size_t size) {
sha1_context_t context;
uint8_t digest[SHA1_DIGEST_SIZE];
sha1_init(&context);
sha1_update(&context, buf, size);
sha1_final(&context, digest);
char strbuf[SHA1_DIGEST_SIZE * 2 + 1];
static char checksum[SHA1_DIGEST_SIZE * 2 + 1];
for (int i = 0; i < SHA1_DIGEST_SIZE; i++) {
snprintf(strbuf, sizeof(strbuf), "%s", checksum);
snprintf(checksum, sizeof(checksum), "%s%02X", strbuf, digest[i]);
}
return checksum;
}
static uint32_t crc32_tab[] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};
uint32_t crc32(uint32_t crc, const void *buf, size_t size) {
// Return a CRC32 checksum
const uint8_t *p;
p = buf;
crc = crc ^ ~0U;
while (size--) {
crc = crc32_tab[(crc ^ *p++) & 0xff] ^ (crc >> 8);
}
return crc ^ ~0U;
}

37
core/hash.h Normal file
View file

@ -0,0 +1,37 @@
#ifndef HASH_H
#define HASH_H
#define SHA1_DIGEST_SIZE 20
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
#ifdef WORDS_BIGENDIAN
#define blk0(i) block->l[i]
#else
#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
|(rol(block->l[i],8)&0x00FF00FF))
#endif
#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
^block->l[(i+2)&15]^block->l[i&15],1))
#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
typedef struct {
uint32_t state[5];
uint32_t count[2];
uint8_t buffer[64];
} sha1_context_t;
const char* sha1(const void *buf, size_t size);
void sha1_init(sha1_context_t* context);
void sha1_transform(uint32_t state[5], const uint8_t buffer[64]);
void sha1_update(sha1_context_t* context, const uint8_t* data, const size_t len);
void sha1_final(sha1_context_t* context, uint8_t digest[SHA1_DIGEST_SIZE]);
uint32_t crc32(uint32_t crc, const void *buf, size_t size);
#endif

View file

@ -16,12 +16,18 @@ typedef struct {
} rominfo_t;
rominfo_t game_list[] = {
{0x29822980, MAPPER_CODIES, DISPLAY_PAL, TERRITORY_EXPORT, "Cosmic Spacehead"},
{0xB9664AE1, MAPPER_CODIES, DISPLAY_PAL, TERRITORY_EXPORT, "Fantastic Dizzy"},
{0xA577CE46, MAPPER_CODIES, DISPLAY_PAL, TERRITORY_EXPORT, "Micro Machines"},
{0x8813514B, MAPPER_CODIES, DISPLAY_PAL, TERRITORY_EXPORT, "Excellent Dizzy (Proto)"},
{0xAA140C9C, MAPPER_CODIES, DISPLAY_PAL, TERRITORY_EXPORT, "Excellent Dizzy (Proto - GG)"},
{-1 , -1 , -1, -1, NULL},
{0x17AB6883, MAPPER_NONE , DISPLAY_NTSC, TERRITORY_EXPORT, "FA Tetris (KR)" },
{0x61E8806F, MAPPER_NONE , DISPLAY_NTSC, TERRITORY_EXPORT, "Flash Point (KR)" },
{0x192949D5, MAPPER_KOREA2, DISPLAY_NTSC, TERRITORY_EXPORT, "Janggun-iuo Adeul (KR)" },
{0xA05258F5, MAPPER_KOREA , DISPLAY_NTSC, TERRITORY_EXPORT, "Won-Si-In (KR)" },
{0x83F0EEDE, MAPPER_KOREA , DISPLAY_NTSC, TERRITORY_EXPORT, "Street Master (KR)" },
{0x445525E2, MAPPER_KOREA , DISPLAY_NTSC, TERRITORY_EXPORT, "Penguin Adventure (KR)" },
{0x29822980, MAPPER_CODIES, DISPLAY_PAL, TERRITORY_EXPORT, "Cosmic Spacehead" },
{0xB9664AE1, MAPPER_CODIES, DISPLAY_PAL, TERRITORY_EXPORT, "Fantastic Dizzy" },
{0xA577CE46, MAPPER_CODIES, DISPLAY_PAL, TERRITORY_EXPORT, "Micro Machines" },
{0x8813514B, MAPPER_CODIES, DISPLAY_PAL, TERRITORY_EXPORT, "Excellent Dizzy (Proto)" },
{0xAA140C9C, MAPPER_CODIES, DISPLAY_PAL, TERRITORY_EXPORT, "Excellent Dizzy (Proto - GG)" },
{-1 , -1 , -1 , -1 , NULL },
};
int load_rom(char *filename)

View file

@ -3,7 +3,6 @@
/* Function prototypes */
int load_rom(char *filename);
unsigned char *loadzip(char *archive, char *filename, int *filesize);
extern char game_name[PATH_MAX];

View file

@ -4,6 +4,10 @@
*/
#include "shared.h"
void (*cpu_writemem16)(int address, int data);
void (*cpu_writeport16)(uint16_t port, uint8_t data);
uint8_t (*cpu_readport16)(uint16_t port);
/* Pull-up resistors on data bus */
uint8_t data_bus_pullup = 0x00;
uint8_t data_bus_pulldown = 0x00;

View file

@ -5,8 +5,6 @@
#include "shared.h"
#include "stdint.h"
uint8_t sms_cram_expand_table[4];
uint8_t gg_cram_expand_table[16];
@ -21,28 +19,18 @@ uint8_t *linebuf;
uint8_t internal_buffer[0x100];
/* Precalculated pixel table */
uint16_t pixel[PALETTE_SIZE];
uint32_t pixel[PALETTE_SIZE];
/* Dirty pattern info */
uint8_t bg_name_dirty[0x200]; /* 1= This pattern is dirty */
uint16_t bg_name_list[0x200]; /* List of modified pattern indices */
uint16_t bg_list_index; /* # of modified patterns in list */
uint8_t bg_pattern_cache[0x20000];/* Cached and flipped patterns */
/* Pixel look-up table */
uint8_t lut[0x10000];
/* Attribute expansion table */
static const uint32_t atex[4] =
{
0x00000000,
0x10101010,
0x20202020,
0x30303030,
};
/* Bitplane to packed pixel LUT */
uint32_t bp_lut[0x10000];
uint32_t bp_lut[0x20000];
/* Macros to access memory 32-bits at a time (from MAME's drawgfx.c) */
@ -184,15 +172,20 @@ void render_init(void)
{
int x;
uint32_t out = 0;
uint32_t outr = 0;
for(x = 0; x < 8; x++)
{
out |= (j & (0x80 >> x)) ? (uint32_t)(8 << (x << 2)) : 0;
out |= (i & (0x80 >> x)) ? (uint32_t)(4 << (x << 2)) : 0;
out |= (j & (0x80 >> ( x))) ? (uint32_t)(8 << (x << 2)) : 0;
out |= (i & (0x80 >> ( x))) ? (uint32_t)(4 << (x << 2)) : 0;
outr |= (j & (0x80 >> (7-x))) ? (uint32_t)(8 << (x << 2)) : 0;
outr |= (i & (0x80 >> (7-x))) ? (uint32_t)(4 << (x << 2)) : 0;
}
#if LSB_FIRST
bp_lut[(j << 8) | (i)] = out;
bp_lut[0x00000 | (j << 8) | (i)] = out;
bp_lut[0x10000 | (j << 8) | (i)] = outr;
#else
bp_lut[(i << 8) | (j)] = out;
bp_lut[0x00000 | (i << 8) | (j)] = out;
bp_lut[0x10000 | (i << 8) | (j)] = outr;
#endif
}
@ -227,12 +220,6 @@ void render_reset(void)
palette_sync(i, 1);
}
/* Invalidate pattern cache */
memset(bg_name_dirty, 0, sizeof(bg_name_dirty));
memset(bg_name_list, 0, sizeof(bg_name_list));
bg_list_index = 0;
memset(bg_pattern_cache, 0, sizeof(bg_pattern_cache));
/* Pick render routine */
render_bg = render_bg_sms;
render_obj = render_obj_sms;
@ -247,10 +234,7 @@ void render_line(int line)
return;
/* Point to current line in output buffer */
linebuf = (bitmap.depth == 8) ? &bitmap.data[(line * bitmap.pitch)] : &internal_buffer[0];
/* Update pattern cache */
update_bg_pattern_cache();
linebuf = &internal_buffer[0];
/* Blank line (full width) */
if(!(vdp.reg[1] & 0x40))
@ -274,37 +258,51 @@ void render_line(int line)
}
}
if(bitmap.depth != 8) remap_8_to_16(line);
remap_internal_to_host(line);
}
static inline uint32_t get_tile_row(uint16_t attr, int row)
{
int tile_row = (attr & 0x0400) ? (row ^ 7) : (row);
uint32_t *bp_ptr = (attr & 0x200) ? &bp_lut[0x10000] : &bp_lut[0];
uint32_t key = *(uint32_t *)&vdp.vram[((attr & 0x01FF) << 5) | (tile_row << 2)];
uint32_t temp = bp_ptr[key & 0xFFFF] >> 2;
return temp | bp_ptr[key >> 16];
}
static inline uint32_t get_sprite_tile_row(uint16_t attr, int row)
{
uint32_t key = *(uint32_t *)&vdp.vram[((attr & 0x01FF) << 5) | (row << 2)];
uint32_t temp = bp_lut[key & 0xFFFF] >> 2;
return temp | bp_lut[key >> 16];
}
/* Draw the Master System background */
void render_bg_sms(int line)
{
int locked = 0;
int yscroll_mask = (vdp.extended) ? 256 : 224;
int v_line = (line + vdp.reg[9]) % yscroll_mask;
int v_row = (v_line & 7) << 3;
int v_row = (v_line & 7);
int hscroll = ((vdp.reg[0] & 0x40) && (line < 0x10)) ? 0 : (0x100 - vdp.reg[8]);
int column = 0;
uint16_t attr;
uint16_t *nt = (uint16_t *)&vdp.vram[vdp.ntab + ((v_line >> 3) << 6)];
int nt_scroll = (hscroll >> 3);
int shift = (hscroll & 7);
uint32_t atex_mask;
uint32_t *cache_ptr;
uint32_t *linebuf_ptr = (uint32_t *)&linebuf[0 - shift];
uint8_t *linebuf_ptr = (uint8_t *)&linebuf[0 - shift];
/* Draw first column (clipped) */
if(shift)
{
int x;
for(x = shift; x < 8; x++)
linebuf[(0 - shift) + (x)] = 0;
column++;
}
linebuf_ptr = &linebuf_ptr[(column << 3)];
/* Draw a line of the background */
for(; column < 32; column++)
@ -313,7 +311,7 @@ void render_bg_sms(int line)
if((vdp.reg[0] & 0x80) && (!locked) && (column >= 24))
{
locked = 1;
v_row = (line & 7) << 3;
v_row = (line & 7);
nt = (uint16_t *)&vdp.vram[((vdp.reg[2] << 10) & 0x3800) + ((line >> 3) << 6)];
}
@ -323,37 +321,36 @@ void render_bg_sms(int line)
#ifndef LSB_FIRST
attr = (((attr & 0xFF) << 8) | ((attr & 0xFF00) >> 8));
#endif
/* Expand priority and palette bits */
atex_mask = atex[(attr >> 11) & 3];
uint8_t atex = ((attr >> 11) & 3) << 4;
uint32_t temp = get_tile_row(attr, v_row);
/* Point to a line of pattern data in cache */
cache_ptr = (uint32_t *)&bg_pattern_cache[((attr & 0x7FF) << 6) | (v_row)];
/* Copy the left half, adding the attribute bits in */
write_dword( &linebuf_ptr[(column << 1)] , read_dword( &cache_ptr[0] ) | (atex_mask));
/* Copy the right half, adding the attribute bits in */
write_dword( &linebuf_ptr[(column << 1) | (1)], read_dword( &cache_ptr[1] ) | (atex_mask));
*linebuf_ptr++ = (temp & 0x0F) | atex; temp >>= 4;
*linebuf_ptr++ = (temp & 0x0F) | atex; temp >>= 4;
*linebuf_ptr++ = (temp & 0x0F) | atex; temp >>= 4;
*linebuf_ptr++ = (temp & 0x0F) | atex; temp >>= 4;
*linebuf_ptr++ = (temp & 0x0F) | atex; temp >>= 4;
*linebuf_ptr++ = (temp & 0x0F) | atex; temp >>= 4;
*linebuf_ptr++ = (temp & 0x0F) | atex; temp >>= 4;
*linebuf_ptr++ = (temp & 0x0F) | atex;
}
/* Draw last column (clipped) */
if(shift)
{
int x, c, a;
uint8_t *p = &linebuf[(0 - shift)+(column << 3)];
int x;
uint8_t *p = &linebuf[(0 - shift) + (column << 3)];
attr = nt[(column + nt_scroll) & 0x1F];
#ifndef LSB_FIRST
attr = (((attr & 0xFF) << 8) | ((attr & 0xFF00) >> 8));
#endif
a = (attr >> 7) & 0x30;
uint8_t atex = ((attr >> 11) & 3) << 4;
uint32_t temp = get_tile_row(attr, v_row);
for(x = 0; x < shift; x++)
{
c = bg_pattern_cache[((attr & 0x7FF) << 6) | (v_row) | (x)];
p[x] = ((c) | (a));
p[x] = (temp & 0x0F) | atex;
temp >>= 4;
}
}
}
@ -452,14 +449,21 @@ void render_obj_sms(int line)
/* Draw double size sprite */
if(vdp.reg[1] & 0x01)
{
int toggle = 0;
int x;
uint8_t *cache_ptr = (uint8_t *)&bg_pattern_cache[(n << 6) | (((line - yp) >> 1) << 3)];
uint32_t temp = get_sprite_tile_row(n, (line - yp) >> 1);
/* Pre-shift line */
temp >>= ((start >> 1) << 2);
/* Draw sprite line */
for(x = start; x < end; x++)
{
/* Source pixel from cache */
uint8_t sp = cache_ptr[(x >> 1)];
/* Source pixel */
uint8_t sp = (temp & 0x0F);
if(toggle)
temp >>= 4;
toggle ^= 1;
/* Only draw opaque sprite pixels */
if(sp)
@ -478,13 +482,17 @@ void render_obj_sms(int line)
else /* Regular size sprite (8x8 / 8x16) */
{
int x;
uint8_t *cache_ptr = (uint8_t *)&bg_pattern_cache[(n << 6) | ((line - yp) << 3)];
uint32_t temp = get_sprite_tile_row(n, line - yp);
/* Pre-shift line */
temp >>= (start << 2);
/* Draw sprite line */
for(x = start; x < end; x++)
{
/* Source pixel from cache */
uint8_t sp = cache_ptr[x];
/* Source pixel */
uint8_t sp = (temp & 0x0F);
temp >>= 4;
/* Only draw opaque sprite pixels */
if(sp)
@ -509,46 +517,6 @@ end:
}
void update_bg_pattern_cache(void)
{
int i;
uint8_t x, y;
uint16_t name;
if(!bg_list_index) return;
for(i = 0; i < bg_list_index; i++)
{
name = bg_name_list[i];
bg_name_list[i] = 0;
for(y = 0; y < 8; y++)
{
if(bg_name_dirty[name] & (1 << y))
{
uint8_t *dst = &bg_pattern_cache[name << 6];
uint16_t bp01 = *(uint16_t *)&vdp.vram[(name << 5) | (y << 2) | (0)];
uint16_t bp23 = *(uint16_t *)&vdp.vram[(name << 5) | (y << 2) | (2)];
uint32_t temp = (bp_lut[bp01] >> 2) | (bp_lut[bp23]);
for(x = 0; x < 8; x++)
{
uint8_t c = (temp >> (x << 2)) & 0x0F;
dst[0x00000 | (y << 3) | (x)] = (c);
dst[0x08000 | (y << 3) | (x ^ 7)] = (c);
dst[0x10000 | ((y ^ 7) << 3) | (x)] = (c);
dst[0x18000 | ((y ^ 7) << 3) | (x ^ 7)] = (c);
}
}
}
bg_name_dirty[name] = 0;
}
bg_list_index = 0;
}
/* Update a palette entry */
void palette_sync(int index, int force)
{
@ -583,20 +551,14 @@ void palette_sync(int index, int force)
g = sms_cram_expand_table[g];
b = sms_cram_expand_table[b];
}
bitmap.pal.color[index][0] = r;
bitmap.pal.color[index][1] = g;
bitmap.pal.color[index][2] = b;
pixel[index] = MAKE_PIXEL(r, g, b);
bitmap.pal.dirty[index] = bitmap.pal.update = 1;
}
void remap_8_to_16(int line)
void remap_internal_to_host(int line)
{
int i;
uint16_t *p = (uint16_t *)&bitmap.data[(line * bitmap.pitch)];
uint32_t *p = (uint32_t *)&bitmap.data[(line * bitmap.pitch)];
for(i = bitmap.viewport.x; i < bitmap.viewport.w + bitmap.viewport.x; i++)
{
p[i] = pixel[ internal_buffer[i] & PIXEL_MASK ];

View file

@ -2,31 +2,28 @@
#ifndef _RENDER_H_
#define _RENDER_H_
/* Pack RGB data into a 16-bit RGB 5:6:5 format */
#define MAKE_PIXEL(r,g,b) (((r << 8) & 0xF800) | ((g << 3) & 0x07E0) | ((b >> 3) & 0x001F))
/* Pack RGB data into a 32-bit RGB 5:6:5 format */
#define MAKE_PIXEL(r,g,b) (r << 16 | g << 8 | b)
/* Used for blanking a line in whole or in part */
#define BACKDROP_COLOR (0x10 | (vdp.reg[7] & 0x0F))
#include "stdint.h"
extern uint8_t sms_cram_expand_table[4];
extern uint8_t gg_cram_expand_table[16];
extern void (*render_bg)(int line);
extern void (*render_obj)(int line);
extern uint8_t *linebuf;
extern uint8_t internal_buffer[0x100];
extern uint16_t pixel[];
extern uint32_t pixel[];
extern uint8_t bg_name_dirty[0x200];
extern uint16_t bg_name_list[0x200];
extern uint16_t bg_list_index;
extern uint8_t bg_pattern_cache[0x20000];
extern uint8_t tms_lookup[16][256][2];
extern uint8_t mc_lookup[16][256][8];
extern uint8_t txt_lookup[256][2];
extern uint8_t bp_expand[256][8];
extern uint8_t lut[0x10000];
extern uint32_t bp_lut[0x10000];
extern uint32_t bp_lut[0x20000];
void render_shutdown(void);
void render_init(void);
@ -34,8 +31,7 @@ void render_reset(void);
void render_line(int line);
void render_bg_sms(int line);
void render_obj_sms(int line);
void update_bg_pattern_cache(void);
void palette_sync(int index, int force);
void remap_8_to_16(int line);
void remap_internal_to_host(int line);
#endif /* _RENDER_H_ */

View file

@ -10,7 +10,6 @@
#include <malloc.h>
#include <math.h>
#include <limits.h>
#include <zlib.h>
#ifndef PATH_MAX
#ifdef MAX_PATH
@ -39,5 +38,6 @@
#include "state.h"
#include "loadrom.h"
#include "hash.h"
#endif /* _SHARED_H_ */

View file

@ -10,6 +10,18 @@ sms_t sms;
uint8_t dummy_write[0x400];
uint8_t dummy_read[0x400];
void read_map(uint8_t *src, int offset, int length)
{
int index;
int page_shift = 10;
int page_count = (length >> page_shift) & 0x3F;
for(index = 0; index < page_count; index++)
{
cpu_readmap[(offset >> page_shift) | index] = &src[index << page_shift];
}
}
void writemem_mapper_none(int offset, int data)
{
cpu_writemap[offset >> 10][offset & 0x03FF] = data;
@ -29,12 +41,15 @@ void writemem_mapper_codies(int offset, int data)
case 0x0000:
sms_mapper_w(1, data);
return;
case 0x4000:
sms_mapper_w(2, data);
return;
case 0x8000:
sms_mapper_w(3, data);
return;
case 0xC000:
cpu_writemap[offset >> 10][offset & 0x03FF] = data;
return;
@ -42,6 +57,83 @@ void writemem_mapper_codies(int offset, int data)
}
void writemem_mapper_korea(int offset, int data)
{
int i;
static const int bank_mask = 0x0F;
static const int bank_shift = 13;
static const int page_mask = 0x0F;
static const int page_shift = 10;
uint8_t *base = &cart.rom[(data & bank_mask) << bank_shift];
switch(offset)
{
/* 4-bit data written to 0000 maps 8K page to 8000-9FFF */
case 0x0000:
for(i = 0; i < 8; i++)
{
cpu_readmap[(0x8000 >> page_shift) + i] = \
&base[(i & page_mask) << page_shift];
}
return;
/* 4-bit data written to 0001 maps 8K page to A000-BFFF */
case 0x0001:
for(i = 0; i < 8; i++)
{
cpu_readmap[(0xA000 >> page_shift) + i] = \
&base[(i & page_mask) << page_shift];
}
return;
/* 4-bit data written to 0002 maps 8K page to 4000-5FFF */
case 0x0002:
for(i = 0; i < 8; i++)
{
cpu_readmap[(0x4000 >> page_shift) + i] = \
&base[(i & page_mask) << page_shift];
}
return;
/* 4-bit data written to 0003 maps 8K page to 6000-7FFF */
case 0x0003:
for(i = 0; i < 8; i++)
{
cpu_readmap[(0x6000 >> page_shift) + i] = \
&base[(i & page_mask) << page_shift];
}
return;
}
cpu_writemap[offset >> 10][offset & 0x03FF] = data;
}
void writemem_mapper_korea2(int offset, int data)
{
int i;
static const int bank_mask = 0x3F;
static const int bank_shift = 13;
static const int page_shift = 10;
uint8_t *base = &cart.rom[(data & bank_mask) << bank_shift];
switch(offset)
{
case 0x4000: /* 6-bit data written to 4000 maps 8K page to 4000-5FFF */
case 0x6000: /* 6-bit data written to 6000 maps 8K page to 6000-7FFF */
case 0x8000: /* 6-bit data written to 8000 maps 8K page to 8000-9FFF */
case 0xA000: /* 6-bit data written to A000 maps 8K page to A000-BFFF */
for(i = 0; i < 8; i++)
{
cpu_readmap[(offset >> page_shift) + i] = \
&base[(i & bank_mask) << page_shift];
}
return;
}
cpu_writemap[offset >> 10][offset & 0x03FF] = data;
}
void sms_init(void)
{
z80_init();
@ -53,9 +145,32 @@ void sms_init(void)
data_bus_pulldown = 0x00;
/* Assign mapper */
cpu_writemem16 = writemem_mapper_sega;
if(cart.mapper == MAPPER_CODIES)
cpu_writemem16 = writemem_mapper_codies;
switch(cart.mapper)
{
case MAPPER_NONE:
cpu_writemem16 = writemem_mapper_none;
break;
case MAPPER_SEGA:
cpu_writemem16 = writemem_mapper_sega;
break;
case MAPPER_KOREA:
cpu_writemem16 = writemem_mapper_korea;
break;
case MAPPER_KOREA2:
cpu_writemem16 = writemem_mapper_korea2;
break;
case MAPPER_CODIES:
cpu_writemem16 = writemem_mapper_codies;
break;
default:
cpu_writemem16 = writemem_mapper_sega;
break;
}
/* Force SMS (J) console type if FM sound enabled */
if(sms.use_fm)

View file

@ -13,7 +13,9 @@ enum {
enum {
MAPPER_NONE = 0,
MAPPER_SEGA = 1,
MAPPER_CODIES = 2
MAPPER_CODIES = 2,
MAPPER_KOREA = 3,
MAPPER_KOREA2 = 4
};
enum {

View file

@ -115,5 +115,7 @@ void system_load_state(void *fd)
/* Restore palette */
for(i = 0; i < PALETTE_SIZE; i++)
palette_sync(i, 1);
viewport_check();
}

View file

@ -3,7 +3,7 @@
#define _SYSTEM_H_
#define APP_NAME "SMS Plus"
#define APP_VERSION "1.3"
#define APP_VERSION "1.8"
#define PALETTE_SIZE 0x20

View file

@ -13,6 +13,7 @@ static const uint8_t tms_crom[] =
0x04, 0x33, 0x15, 0x3F
};
/* Mark a pattern as dirty */
#define MARK_BG_DIRTY(addr) \
{ \
@ -41,6 +42,26 @@ void vdp_shutdown(void)
/* Nothing to do */
}
void set_tms_palette(void)
{
int i;
/* Load TMS9918 palette */
for(i = 0; i < PALETTE_SIZE; i++)
{
int r, g, b;
r = (tms_crom[i & 0x0F] >> 0) & 3;
g = (tms_crom[i & 0x0F] >> 2) & 3;
b = (tms_crom[i & 0x0F] >> 4) & 3;
r = sms_cram_expand_table[r];
g = sms_cram_expand_table[g];
b = sms_cram_expand_table[b];
pixel[i] = MAKE_PIXEL(r, g, b);
}
}
/* Reset VDP emulation */
void vdp_reset(void)
@ -54,6 +75,8 @@ void vdp_reset(void)
bitmap.viewport.w = (IS_GG) ? 160 : 256;
bitmap.viewport.h = (IS_GG) ? 144 : 192;
bitmap.viewport.changed = 1;
set_tms_palette();
}
@ -81,27 +104,7 @@ void viewport_check(void)
}
else
{
/* Load TMS9918 palette */
for(i = 0; i < PALETTE_SIZE; i++)
{
int r, g, b;
r = (tms_crom[i & 0x0F] >> 0) & 3;
g = (tms_crom[i & 0x0F] >> 2) & 3;
b = (tms_crom[i & 0x0F] >> 4) & 3;
r = sms_cram_expand_table[r];
g = sms_cram_expand_table[g];
b = sms_cram_expand_table[b];
bitmap.pal.color[i][0] = r;
bitmap.pal.color[i][1] = g;
bitmap.pal.color[i][2] = b;
pixel[i] = MAKE_PIXEL(r, g, b);
bitmap.pal.dirty[i] = bitmap.pal.update = 1;
}
set_tms_palette();
}
}

View file

@ -1,4 +1,3 @@
#ifndef _VDP_H_
#define _VDP_H_
@ -45,6 +44,10 @@ typedef struct
uint8_t hint_pending;
uint16_t cram_latch;
uint8_t bd;
void (*vram_write)(uint16_t offset, uint8_t data);
uint8_t (*vram_read)(uint16_t offset, uint8_t data);
} vdp_t;
/* Global data */
@ -60,6 +63,7 @@ void vdp_write(int offset, uint8_t data);
void gg_vdp_write(int offset, uint8_t data);
void md_vdp_write(int offset, uint8_t data);
void tms_write(int offset, int data);
void viewport_check(void);
#endif /* _VDP_H_ */

View file

@ -62,9 +62,9 @@ extern int after_EI;
extern unsigned char *cpu_readmap[64];
extern unsigned char *cpu_writemap[64];
void (*cpu_writemem16)(int address, int data);
void (*cpu_writeport16)(uint16_t port, uint8_t data);
uint8_t (*cpu_readport16)(uint16_t port);
extern void (*cpu_writemem16)(int address, int data);
extern void (*cpu_writeport16)(uint16_t port, uint8_t data);
extern uint8_t (*cpu_readport16)(uint16_t port);
void z80_reset_cycle_count(void);
int z80_get_elapsed_cycles(void);