diff --git a/dumpinfo.c b/dumpinfo.c index ae5de09..2189a85 100644 --- a/dumpinfo.c +++ b/dumpinfo.c @@ -1,3 +1,10 @@ +/* + * NewOswan + * dumpinfo.c: Tool to dump the metadata info about a cart rom image. + * + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ #include #include #include @@ -17,7 +24,7 @@ char *load_file(char *filename, uint32_t *fileSize) fstat(fd, &FileStat); - ret_ptr = (char *)mmap(NULL, FileStat.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + ret_ptr = (char *)mmap(NULL, FileStat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); *fileSize = FileStat.st_size; @@ -32,7 +39,7 @@ char *load_file(char *filename, uint32_t *fileSize) } #pragma pack(1) -struct cart_metadata +struct cart_metadata { uint8_t farjump[5]; uint8_t flagExt; @@ -64,29 +71,26 @@ int main(int argc, char *argv[]) if (content != NULL) { - data = (struct cart_metadata*)&(content[size - sizeof(struct cart_metadata)]); + data = (struct cart_metadata *)&(content[size - sizeof(struct cart_metadata)]); printf("%s:\n", argv[1]); if (data->farjump[0] == 0xEA) { - printf("[%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x]", - data->publishId, data->gameId[0], data->gameId[1], data->flags2, data->romInfo, data->saveInfo, - data->flags & 0xFF, (data->flags>>8) & 0xFF, data->crc & 0xFF, (data->crc>>8) & 0xFF); - printf(" - Reset @ %02X%02X:%02X%02Xh\n", - data->farjump[4], data->farjump[3], - data->farjump[2], data->farjump[1]); + printf("[%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x]", data->publishId, data->gameId[0], + data->gameId[1], data->flags2, data->romInfo, data->saveInfo, data->flags & 0xFF, + (data->flags >> 8) & 0xFF, data->crc & 0xFF, (data->crc >> 8) & 0xFF); + printf(" - Reset @ %02X%02X:%02X%02Xh\n", data->farjump[4], data->farjump[3], data->farjump[2], + data->farjump[1]); printf(" - publisher: %02X, gameId: %01X%02X\n", data->publishId, data->gameId[0], data->gameId[1]); - printf(" - %s want to write to EEPROM\n", data->flags2&0x80?"Do":"Do not"); - printf(" - %s user defined bootsplash\n", data->flagExt&0x80?"Dissallow":"Allow"); - printf(" - Is %sbootable on a normal swan\n", data->flagExt&0x0F?"not ":""); + printf(" - %s want to write to EEPROM\n", data->flags2 & 0x80 ? "Do" : "Do not"); + printf(" - %s user defined bootsplash\n", data->flagExt & 0x80 ? "Dissallow" : "Allow"); + printf(" - Is %sbootable on a normal swan\n", data->flagExt & 0x0F ? "not " : ""); printf(" - ROM Size: %02Xh\n", data->romInfo); printf(" - Save type & Size: %02Xh\n", data->saveInfo); - printf(" - Flags: %d cycles ROM, %d bit ROM bus, %sRTC, %s orientation\n", - data->flags & 0x004?1:3, - data->flags & 0x002?8:16, - data->flags & 0x100?"":"No ", - data->flags & 0x001?"Vertical":"Horizontal"); + printf(" - Flags: %d cycles ROM, %d bit ROM bus, %sRTC, %s orientation\n", data->flags & 0x004 ? 1 : 3, + data->flags & 0x002 ? 8 : 16, data->flags & 0x100 ? "" : "No ", + data->flags & 0x001 ? "Vertical" : "Horizontal"); printf(" - CRC: %04Xh\n", data->crc); ret = 0; } diff --git a/main.c b/main.c index e445e5c..c1fcd9f 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,10 @@ -/////////////////////////////////////////////////////////////////////////////// -// Wonderswan emulator +/* + * NewOswan + * main.c: Entry point + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ //////////////////////////////////////////////////////////////////////////////// // // 13.04.2002: Fixed a small bug causing crashes @@ -7,7 +12,7 @@ // // // -////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// #include #include @@ -36,73 +41,65 @@ // // //////////////////////////////////////////////////////////////////////////////// -#define LOG_PATH "oswan.log" +#define LOG_PATH "oswan.log" - -int gui_command=GUI_COMMAND_NONE; -int gui_mainDialogRunning; -int gui_controls_configuration_Running; -int gui_get_key_Running; -int gui_get_key_key; - -int ws_videoEnhancementType=0; int sram_path_explicit = 0; int ieep_path_explicit = 0; int ws_mk_savpath() { - char *w; + char *w; - if (sram_path_explicit) - { - return 0; - } + if (sram_path_explicit) + { + return 0; + } - if (ws_sram_path != NULL) - { - free(ws_sram_path); - } + if (ws_sram_path != NULL) + { + free(ws_sram_path); + } - ws_sram_path = (char *)malloc(strlen(ws_rom_path) + 2); - strcpy(ws_sram_path, ws_rom_path); - w = strrchr(ws_sram_path, '.'); + ws_sram_path = (char *)malloc(strlen(ws_rom_path) + 2); + strcpy(ws_sram_path, ws_rom_path); + w = strrchr(ws_sram_path, '.'); - if (NULL == w) - { - strcpy(ws_sram_path, "error.sav"); - return 1; - } + if (NULL == w) + { + strcpy(ws_sram_path, "error.sav"); + return 1; + } - strcpy(w, ".sav"); - return 0; + strcpy(w, ".sav"); + return 0; } int ws_mk_ieppath() { - char *w; + char *w; - if (ieep_path_explicit) - { - return 0; - } + if (ieep_path_explicit) + { + return 0; + } - if (ws_ieep_path != NULL) - { - free(ws_ieep_path); - } + if (ws_ieep_path != NULL) + { + free(ws_ieep_path); + } - ws_ieep_path = (char *)malloc(strlen(ws_rom_path) + 2); - strcpy(ws_ieep_path, ws_rom_path); - w = strrchr(ws_ieep_path, '.'); + ws_ieep_path = (char *)malloc(strlen(ws_rom_path) + 2); + strcpy(ws_ieep_path, ws_rom_path); + w = strrchr(ws_ieep_path, '.'); - if (NULL == w) - { - strcpy(ws_ieep_path, "error.iep"); - return 1; - } + if (NULL == w) + { + strcpy(ws_ieep_path, "error.iep"); + return 1; + } - strcpy(w, ".iep"); - return 0; + strcpy(w, ".iep"); + return 0; } //////////////////////////////////////////////////////////////////////////////// @@ -130,81 +127,74 @@ int main(int argc, char *argv[]) fprintf(log_get(), "NewOswan %s (built at: %s %s)\n", VERSION, __DATE__, __TIME__); - ws_rom_path = NULL; + ws_rom_path = NULL; - for (int n = 1; n < argc; ++n) - { - if (argv[n][0] == '-') - { - switch(argv[n][1]) - { - case 'C': - if (++n < argc) + for (int n = 1 ; n < argc ; ++n) + { + if (argv[n][0] == '-') + { + switch (argv[n][1]) { - ws_cyclesByLine = atoi(argv[n]); + case 'C': + if (++n < argc) + { + ws_cyclesByLine = atoi(argv[n]); + } + + fprintf(log_get(), "Cycles by line set to %d\n", ws_cyclesByLine); + break; + + case 'w': + if (++n < argc) + { + ws_system = atoi(argv[n]); + } + + fprintf(log_get(), "WonderSwan set to %d\n", ws_system); + break; + + case 's': + if (++n < argc) + { + ws_sram_path = argv[n]; + } + + sram_path_explicit = 1; + break; + + default:break; } + } + else + { + ws_rom_path = argv[n]; + ws_mk_savpath(); + ws_mk_ieppath(); + } + } - fprintf(log_get(), "Cycles by line set to %d\n", ws_cyclesByLine); - break; + while (!app_terminate) + { + if (!ws_rom_path) + { + exit(0); + } - case 'w': - if (++n < argc) + if (ws_rom_path) + { + ws_set_system(ws_system); + if (ws_init(ws_rom_path)) { - ws_system = atoi(argv[n]); + ws_reset(); + + ws_emulate(); } - fprintf(log_get(), "WonderSwan set to %d\n", ws_system); - break; + ws_done(); + } + } - case 's': - if (++n < argc) - { - ws_sram_path = argv[n]; - } - - sram_path_explicit = 1; - break; - - default: - break; - } - } - else - { - ws_rom_path = argv[n]; - ws_mk_savpath(); - ws_mk_ieppath(); - } - } - - while (!app_terminate) - { - if (!ws_rom_path) - { - app_gameRunning=0; - exit(0); - } - - if (ws_rom_path) - { - ws_set_system(ws_system); - if (ws_init(ws_rom_path)) - { - app_rotated=ws_rotated(); - app_gameRunning=1; - - } - - ws_reset(); - - ws_emulate(); - } - - ws_done(); - } - } - - log_done(); - return(0); + log_done(); + return (0); } diff --git a/resource.h b/resource.h deleted file mode 100644 index 9078fe9..0000000 --- a/resource.h +++ /dev/null @@ -1,50 +0,0 @@ -//{{NO_DEPENDENCIES}} -// Microsoft Developer Studio generated include file. -// Used by gui.rc -// -#define IDD_DIALOG_MAIN 101 -#define IDB_BITMAP1 102 -#define IDB_BITMAP2 103 -#define IDC_BUTTON_LOAD 1000 -#define IDC_BUTTON_RESET 1001 -#define IDC_BUTTON_EXIT 1002 -#define IDC_RADIO1 1003 -#define IDC_RADIO_WINDOWED 1003 -#define IDC_RADIO2 1004 -#define IDC_RADIO_FULLSCREEN 1004 -#define IDC_BUTTON_LOADSTATE 1005 -#define IDC_BUTTON_SAVESTATE 1006 -#define IDC_RADIO3 1007 -#define IDC_BUTTON_CONTINUE 1007 -#define IDC_RADIO4 1008 -#define IDC_BUTTON_CONTROLS 1008 -#define IDC_RADIO5 1009 -#define IDC_RADIO_STANDARD_MODE 1009 -#define IDC_RADIO6 1010 -#define IDC_RADIO_DOUBLESIZE_MODE 1010 -#define IDC_RADIO7 1011 -#define IDC_RADIO_SCANLINES_MODE 1011 -#define IDC_RADIO8 1012 -#define IDC_RADIO_50PRCTSCANLINES_MODE 1012 -#define IDC_RADIO9 1013 -#define IDC_RADIO_SPECIAL_MODE 1013 -#define IDC_RADIO_SYSTEM_AUTODETECT 1014 -#define IDC_RADIO_SYSTEM_MONO 1015 -#define IDC_RADIO_SYSTEM_COLOR 1016 -#define IDC_RADIO_2XSAI 1017 -#define IDC_RADIO_SUPER2XSAI 1018 -#define IDC_COLOUR_DEFAULT 1019 -#define IDC_COLOUR_AMBER 1020 -#define IDC_COLOUR_GREEN 1021 -#define IDC_RADIO_SUPEREAGLE 1022 - -// Next default values for new objects -// -#ifdef APSTUDIO_INVOKED -#ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_NEXT_RESOURCE_VALUE 108 -#define _APS_NEXT_COMMAND_VALUE 40001 -#define _APS_NEXT_CONTROL_VALUE 1021 -#define _APS_NEXT_SYMED_VALUE 101 -#endif -#endif diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 2cbd115..6d869ff 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -1,5 +1,5 @@ set(SOURCES audio.c emulate.c gpu.c io.c log.c memory.c rom.c ws.c) -set(HEADERS audio.h emulate.h gpu.h ieeprom.h io.h log.h memory.h rom.h ws.h) +set(HEADERS audio.h emulate.h gpu.h io.h log.h memory.h rom.h ws.h) add_library(wswan ${SOURCES} ${HEADERS}) diff --git a/source/audio.c b/source/audio.c index 15617fc..8bbabca 100644 --- a/source/audio.c +++ b/source/audio.c @@ -1,5 +1,10 @@ -//////////////////////////////////////////////////////////////////////////////// -// Audio +/* + * NewOswan + * audio.c: + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ //////////////////////////////////////////////////////////////////////////////// // // Sound information thanks to toshi (wscamp wonderswan emulator) @@ -29,65 +34,65 @@ #include "io.h" #include "audio.h" -#define SNDP ws_ioRam[0x80] -#define SNDV ws_ioRam[0x88] -#define SNDSWP ws_ioRam[0x8C] -#define SWPSTP ws_ioRam[0x8D] -#define NSCTL ws_ioRam[0x8E] -#define WAVDTP ws_ioRam[0x8F] -#define SNDMOD ws_ioRam[0x90] -#define SNDOUT ws_ioRam[0x91] -#define PCSRL ws_ioRam[0x92] -#define PCSRH ws_ioRam[0x93] -#define DMASL ws_ioRam[0x40] -#define DMASH ws_ioRam[0x41] -#define DMASB ws_ioRam[0x42] -#define DMADB ws_ioRam[0x43] -#define DMADL ws_ioRam[0x44] -#define DMADH ws_ioRam[0x45] -#define DMACL ws_ioRam[0x46] -#define DMACH ws_ioRam[0x47] -#define DMACTL ws_ioRam[0x48] -#define SDMASL ws_ioRam[0x4A] -#define SDMASH ws_ioRam[0x4B] -#define SDMASB ws_ioRam[0x4C] -#define SDMACL ws_ioRam[0x4E] -#define SDMACH ws_ioRam[0x4F] -#define SDMACTL ws_ioRam[0x52] +#define SNDP ws_ioRam[0x80] +#define SNDV ws_ioRam[0x88] +#define SNDSWP ws_ioRam[0x8C] +#define SWPSTP ws_ioRam[0x8D] +#define NSCTL ws_ioRam[0x8E] +#define WAVDTP ws_ioRam[0x8F] +#define SNDMOD ws_ioRam[0x90] +#define SNDOUT ws_ioRam[0x91] +#define PCSRL ws_ioRam[0x92] +#define PCSRH ws_ioRam[0x93] +#define DMASL ws_ioRam[0x40] +#define DMASH ws_ioRam[0x41] +#define DMASB ws_ioRam[0x42] +#define DMADB ws_ioRam[0x43] +#define DMADL ws_ioRam[0x44] +#define DMADH ws_ioRam[0x45] +#define DMACL ws_ioRam[0x46] +#define DMACH ws_ioRam[0x47] +#define DMACTL ws_ioRam[0x48] +#define SDMASL ws_ioRam[0x4A] +#define SDMASH ws_ioRam[0x4B] +#define SDMASB ws_ioRam[0x4C] +#define SDMACL ws_ioRam[0x4E] +#define SDMACH ws_ioRam[0x4F] +#define SDMACTL ws_ioRam[0x52] -#define BPS 44100 -#define BPSMAX AUDIO_MAX_FREQUENCY -#define BPSMIN AUDIO_MIN_FREQUENCY -#define BUFSIZE 1024 -#define BUFSIZEN 0x10000 -#define BUFSIZEP 159 -#define PCMNUM 100 -#define POFF 128 -#define PDIV 3 -#define PH POFF+PDIV*8 -#define PL POFF-PDIV*7 +#define BPS 44100 +#define BPSMAX AUDIO_MAX_FREQUENCY +#define BPSMIN AUDIO_MIN_FREQUENCY +#define BUFSIZE 1024 +#define BUFSIZEN 0x10000 +#define BUFSIZEP 159 +#define PCMNUM 100 +#define POFF 128 +#define PDIV 3 +#define PH POFF+PDIV*8 +#define PL POFF-PDIV*7 -int WaveMap; -int ChPerInit; -int SwpTime; -int SwpStep; -unsigned int SwpCurPeriod; +int WaveMap; +int ChPerInit; +int SwpTime; +int SwpStep; +unsigned int SwpCurPeriod; -int MainVol=15; -int HardVol=3; +int MainVol = 15; +int HardVol = 3; -int ChCurVol[6]= {-1,-1,-1,-1,-1,-1}; -int ChCurPer[6]= {-1,-1,-1,-1,-1,-1}; -long ChCurPan[6]= {-1,-1,-1,-1,-1,-1}; +int ChCurVol[6] = {-1, -1, -1, -1, -1, -1}; +int ChCurPer[6] = {-1, -1, -1, -1, -1, -1}; +long ChCurPan[6] = {-1, -1, -1, -1, -1, -1}; unsigned char PData[4][BUFSIZE]; -unsigned char PDataP[BUFSIZEP<<4]; +unsigned char PDataP[BUFSIZEP << 4]; unsigned char PDataN[8][BUFSIZEN]; int RandData[BUFSIZEN]; -int CntSwp=0; -int PcmWrPos=0; +int CntSwp = 0; +int PcmWrPos = 0; /* const long TblChVol[16]= // n/15 n=0~15 @@ -121,9 +126,10 @@ const long TblMainVol[4]= // 1,1/2,1/4,1/8 //AUDIOWAVE ws_audio_noise_wave; //AUDIOWAVE ws_audio_sweep_wave; -uint32_t ws_audio_channel_isPlaying[6]; +uint32_t ws_audio_channel_isPlaying[6]; static unsigned int ws_audio_log; + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -137,12 +143,13 @@ static unsigned int ws_audio_log; //////////////////////////////////////////////////////////////////////////////// void ws_audio_init(void) { - fprintf(log_get(), "audio init\n"); - fflush(log_get()); - ws_audio_log=0; - //ws_audio_seal_init(); - ws_audio_reset(); + fprintf(log_get(), "audio init\n"); + fflush(log_get()); + ws_audio_log = 0; + //ws_audio_seal_init(); + ws_audio_reset(); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -156,25 +163,26 @@ void ws_audio_init(void) //////////////////////////////////////////////////////////////////////////////// void ws_audio_reset(void) { - WaveMap=-1; + WaveMap = -1; - for (int i=0; i<6; i++) - { - ws_audio_stop_channel(i); - ws_audio_play_channel(i); - ws_audio_set_channel_frequency(i,0); + for (int i = 0 ; i < 6 ; i++) + { + ws_audio_stop_channel(i); + ws_audio_play_channel(i); + ws_audio_set_channel_frequency(i, 0); - if (i!=4) - { - ws_audio_set_channel_pan(i,0,0); - } + if (i != 4) + { + ws_audio_set_channel_pan(i, 0, 0); + } - ws_audio_clear_channel(i); - } + ws_audio_clear_channel(i); + } - ws_audio_set_channel_frequency(4,0); - ws_audio_set_channel_frequency(4,1792); + ws_audio_set_channel_frequency(4, 0); + ws_audio_set_channel_frequency(4, 1792); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -188,219 +196,220 @@ void ws_audio_reset(void) //////////////////////////////////////////////////////////////////////////////// void ws_audio_port_write(uint32_t port, uint8_t value) { - uint32_t n,i,j,k,b; + uint32_t n, i, j, k, b; - ws_ioRam[port]=value; + ws_ioRam[port] = value; - switch (port) - { - case 0x48: - if (value&0x80) - { - n=(DMACH<<8)|DMACL; - i=(DMASB<<16)|(DMASH<<8)|DMASL; - j=(DMADH<<8)|DMADL; + switch (port) + { + case 0x48: + if (value & 0x80) + { + n = (DMACH << 8) | DMACL; + i = (DMASB << 16) | (DMASH << 8) | DMASL; + j = (DMADH << 8) | DMADL; - for(k=0; k>16)&0xFF); - DMASH=(uint8_t)((i>>8)&0xFF); - DMASL=(uint8_t)(i&0xFF); - DMADB=(uint8_t)((j>>16)&0xFF); - DMADH=(uint8_t)((j>>8)&0xFF); - DMADL=(uint8_t)(j&0xFF); - DMACH=(uint8_t)((n>>8)&0xFF); - DMACL=(uint8_t)(n&0xFF); - value&=0x7F; - } + n = 0; + DMASB = (uint8_t)((i >> 16) & 0xFF); + DMASH = (uint8_t)((i >> 8) & 0xFF); + DMASL = (uint8_t)(i & 0xFF); + DMADB = (uint8_t)((j >> 16) & 0xFF); + DMADH = (uint8_t)((j >> 8) & 0xFF); + DMADL = (uint8_t)(j & 0xFF); + DMACH = (uint8_t)((n >> 8) & 0xFF); + DMACL = (uint8_t)(n & 0xFF); + value &= 0x7F; + } - break; + break; - case 0x80: - case 0x81: - i=(((unsigned int)ws_ioRam[0x81])<<8)+((unsigned int)ws_ioRam[0x80]); - ws_audio_set_channel_frequency(0,i); - break; + case 0x80: + case 0x81: + i = (((unsigned int)ws_ioRam[0x81]) << 8) + ((unsigned int)ws_ioRam[0x80]); + ws_audio_set_channel_frequency(0, i); + break; - case 0x82: - case 0x83: - i=(((unsigned int)ws_ioRam[0x83])<<8)+((unsigned int)ws_ioRam[0x82]); - ws_audio_set_channel_frequency(1,i); - break; + case 0x82: + case 0x83: + i = (((unsigned int)ws_ioRam[0x83]) << 8) + ((unsigned int)ws_ioRam[0x82]); + ws_audio_set_channel_frequency(1, i); + break; - case 0x84: - case 0x85: - i=(((unsigned int)ws_ioRam[0x85])<<8)+((unsigned int)ws_ioRam[0x84]); - ws_audio_set_channel_frequency(2,i); - break; + case 0x84: + case 0x85: + i = (((unsigned int)ws_ioRam[0x85]) << 8) + ((unsigned int)ws_ioRam[0x84]); + ws_audio_set_channel_frequency(2, i); + break; - case 0x86: - case 0x87: - i=(((unsigned int)(ws_ioRam[0x87]&0x07))<<8)+((unsigned int)ws_ioRam[0x86]); - ws_audio_set_channel_frequency(5,i); - ws_audio_set_channel_frequency(3,i); - break; + case 0x86: + case 0x87: + i = (((unsigned int)(ws_ioRam[0x87] & 0x07)) << 8) + ((unsigned int)ws_ioRam[0x86]); + ws_audio_set_channel_frequency(5, i); + ws_audio_set_channel_frequency(3, i); + break; - case 0x88: - ws_audio_set_channel_pan(0,(value&0xF0)>>4,value&0x0F); - break; + case 0x88: + ws_audio_set_channel_pan(0, (value & 0xF0) >> 4, value & 0x0F); + break; - case 0x89: - ws_audio_set_channel_pan(1,(value&0xF0)>>4,value&0x0F); - break; + case 0x89: + ws_audio_set_channel_pan(1, (value & 0xF0) >> 4, value & 0x0F); + break; - case 0x8A: - ws_audio_set_channel_pan(2,(value&0xF0)>>4,value&0x0F); - break; + case 0x8A: + ws_audio_set_channel_pan(2, (value & 0xF0) >> 4, value & 0x0F); + break; - case 0x8B: - ws_audio_set_channel_pan(5,(value&0xF0)>>4,value&0x0F); - ws_audio_set_channel_pan(3,(value&0xF0)>>4,value&0x0F); - break; + case 0x8B: + ws_audio_set_channel_pan(5, (value & 0xF0) >> 4, value & 0x0F); + ws_audio_set_channel_pan(3, (value & 0xF0) >> 4, value & 0x0F); + break; - case 0x8C: - SwpStep=(signed char)value; - break; + case 0x8C: + SwpStep = (signed char)value; + break; - case 0x8D: - SwpTime=(((unsigned int)value)+1)<<5; - break; + case 0x8D: + SwpTime = (((unsigned int)value) + 1) << 5; + break; - case 0x8E: - if (value & 0x10) - { - ws_audio_set_channel_pdata(5,value&0x07); - } - else - { - // hmmm.. shut up! - } + case 0x8E: + if (value & 0x10) + { + ws_audio_set_channel_pdata(5, value & 0x07); + } + else + { + // hmmm.. shut up! + } - break; + break; - case 0x8F: - WaveMap=((unsigned int)value)<<6; - break; + case 0x8F: + WaveMap = ((unsigned int)value) << 6; + break; - case 0x90: - if (value&0x01) - { - ws_audio_play_channel(0); - } - else - { - ws_audio_stop_channel(0); - } + case 0x90: + if (value & 0x01) + { + ws_audio_play_channel(0); + } + else + { + ws_audio_stop_channel(0); + } - if ((value&0x22)==0x02) - { - ws_audio_play_channel(1); - } - else - { - ws_audio_stop_channel(1); - } + if ((value & 0x22) == 0x02) + { + ws_audio_play_channel(1); + } + else + { + ws_audio_stop_channel(1); + } - if (value&0x04) - { - ws_audio_play_channel(2); - } - else - { - ws_audio_stop_channel(2); - } + if (value & 0x04) + { + ws_audio_play_channel(2); + } + else + { + ws_audio_stop_channel(2); + } - if ((value&0x88)==0x08) - { - ws_audio_play_channel(3); - } - else - { - ws_audio_stop_channel(3); - } + if ((value & 0x88) == 0x08) + { + ws_audio_play_channel(3); + } + else + { + ws_audio_stop_channel(3); + } - if ((value&0x88)==0x88) - { - ws_audio_play_channel(5); - } - else - { - ws_audio_stop_channel(5); - } + if ((value & 0x88) == 0x88) + { + ws_audio_play_channel(5); + } + else + { + ws_audio_stop_channel(5); + } - break; + break; - case 0x91: - value|=0x80; - HardVol = (value>>1)&0x3; + case 0x91: + value |= 0x80; + HardVol = (value >> 1) & 0x3; - ws_ioRam[port]=value; // Always have external speaker + ws_ioRam[port] = value; // Always have external speaker - value=ws_ioRam[0x88]; + value = ws_ioRam[0x88]; - ws_audio_set_channel_pan(0,(value&0xF0)>>4,value&0x0F); + ws_audio_set_channel_pan(0, (value & 0xF0) >> 4, value & 0x0F); - value=ws_ioRam[0x89]; + value = ws_ioRam[0x89]; - ws_audio_set_channel_pan(1,(value&0xF0)>>4,value&0x0F); + ws_audio_set_channel_pan(1, (value & 0xF0) >> 4, value & 0x0F); - value=ws_ioRam[0x8A]; + value = ws_ioRam[0x8A]; - ws_audio_set_channel_pan(2,(value&0xF0)>>4,value&0x0F); + ws_audio_set_channel_pan(2, (value & 0xF0) >> 4, value & 0x0F); - value=ws_ioRam[0x8B]; + value = ws_ioRam[0x8B]; - ws_audio_set_channel_pan(3,(value&0xF0)>>4,value&0x0F); + ws_audio_set_channel_pan(3, (value & 0xF0) >> 4, value & 0x0F); - ws_audio_set_channel_pan(5,(value&0xF0)>>4,value&0x0F); + ws_audio_set_channel_pan(5, (value & 0xF0) >> 4, value & 0x0F); - break; + break; - case 0x92: - dbgprintf("0x92 <- 0x%2x\n", value); - fflush(stdout); - break; + case 0x92: + dbgprintf("0x92 <- 0x%2x\n", value); + fflush(stdout); + break; - case 0x93: - dbgprintf("0x93 <- 0x%2x\n", value); - fflush(stdout); - break; + case 0x93: + dbgprintf("0x93 <- 0x%2x\n", value); + fflush(stdout); + break; - case 0x94: - dbgprintf("0x94 <- 0x%2x\n", value); - fflush(stdout); - MainVol=(value&0x0f)>>2; - value=ws_ioRam[0x88]; + case 0x94: + dbgprintf("0x94 <- 0x%2x\n", value); + fflush(stdout); + MainVol = (value & 0x0f) >> 2; + value = ws_ioRam[0x88]; - ws_audio_set_channel_pan(0,(value&0xF0)>>4,value&0x0F); + ws_audio_set_channel_pan(0, (value & 0xF0) >> 4, value & 0x0F); - value=ws_ioRam[0x89]; + value = ws_ioRam[0x89]; - ws_audio_set_channel_pan(1,(value&0xF0)>>4,value&0x0F); + ws_audio_set_channel_pan(1, (value & 0xF0) >> 4, value & 0x0F); - value=ws_ioRam[0x8A]; + value = ws_ioRam[0x8A]; - ws_audio_set_channel_pan(2,(value&0xF0)>>4,value&0x0F); + ws_audio_set_channel_pan(2, (value & 0xF0) >> 4, value & 0x0F); - value=ws_ioRam[0x8B]; + value = ws_ioRam[0x8B]; - ws_audio_set_channel_pan(3,(value&0xF0)>>4,value&0x0F); + ws_audio_set_channel_pan(3, (value & 0xF0) >> 4, value & 0x0F); - ws_audio_set_channel_pan(5,(value&0xF0)>>4,value&0x0F); + ws_audio_set_channel_pan(5, (value & 0xF0) >> 4, value & 0x0F); - break; + break; - case 0x9A: - break; - } + case 0x9A: + break; + } } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -414,8 +423,9 @@ void ws_audio_port_write(uint32_t port, uint8_t value) //////////////////////////////////////////////////////////////////////////////// uint8_t ws_audio_port_read(uint8_t port) { - return(ws_ioRam[port]); + return (ws_ioRam[port]); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -429,8 +439,9 @@ uint8_t ws_audio_port_read(uint8_t port) //////////////////////////////////////////////////////////////////////////////// void ws_audio_done(void) { - ws_audio_seal_done(); + ws_audio_seal_done(); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -445,84 +456,82 @@ void ws_audio_done(void) unsigned int ws_audio_mrand(unsigned int Degree) { #define BIT(n) (1<N!=Degree) - { - pTbl=TblMask; + if (pTbl->N != Degree) + { + pTbl = TblMask; - while(pTbl->N) - { - if(pTbl->N==Degree) - { - break; - } + while (pTbl->N) + { + if (pTbl->N == Degree) + { + break; + } - pTbl++; - } + pTbl++; + } - if(!pTbl->N) - { - pTbl--; - } + if (!pTbl->N) + { + pTbl--; + } - ShiftReg&=pTbl->InputBit-1; + ShiftReg &= pTbl->InputBit - 1; - if(!ShiftReg) - { - ShiftReg=pTbl->InputBit-1; - } - } + if (!ShiftReg) + { + ShiftReg = pTbl->InputBit - 1; + } + } - Masked=ShiftReg&pTbl->Mask; + Masked = ShiftReg & pTbl->Mask; - while(Masked) - { - XorReg^=Masked&0x01; - Masked>>=1; - } + while (Masked) + { + XorReg ^= Masked & 0x01; + Masked >>= 1; + } - if(XorReg) - { - ShiftReg|=pTbl->InputBit; - } - else - { - ShiftReg&=~pTbl->InputBit; - } + if (XorReg) + { + ShiftReg |= pTbl->InputBit; + } + else + { + ShiftReg &= ~pTbl->InputBit; + } - ShiftReg>>=1; + ShiftReg >>= 1; - return ShiftReg; + return ShiftReg; } @@ -540,148 +549,149 @@ unsigned int ws_audio_mrand(unsigned int Degree) int ws_audio_seal_init(void) { #if 0 - int i, j; - AUDIOINFO info; - AUDIOCAPS caps; - uint32_t rc; - uint32_t nDevId; + int i, j; + AUDIOINFO info; + AUDIOCAPS caps; + uint32_t rc; + uint32_t nDevId; - fprintf(log_get(),"audio: using seal audio library\n"); - /* initialize audio library */ - AInitialize(); + fprintf(log_get(),"audio: using seal audio library\n"); + /* initialize audio library */ + AInitialize(); - /* show registered device drivers */ - fprintf(log_get(),"audio: registered sound devices:\n"); + /* show registered device drivers */ + fprintf(log_get(),"audio: registered sound devices:\n"); - for (nDevId = 0; nDevId < AGetAudioNumDevs(); nDevId++) - { - AGetAudioDevCaps(nDevId, &caps); - fprintf(log_get(),"audio: %2d. %s\n", nDevId, caps.szProductName); - } + for (nDevId = 0; nDevId < AGetAudioNumDevs(); nDevId++) + { + AGetAudioDevCaps(nDevId, &caps); + fprintf(log_get(),"audio: %2d. %s\n", nDevId, caps.szProductName); + } - /* open audio device */ - info.nDeviceId = 0; - info.wFormat = AUDIO_FORMAT_16BITS | AUDIO_FORMAT_STEREO; // | AUDIO_MIXER_BASS; - info.nSampleRate = 44100; + /* open audio device */ + info.nDeviceId = 0; + info.wFormat = AUDIO_FORMAT_16BITS | AUDIO_FORMAT_STEREO; // | AUDIO_MIXER_BASS; + info.nSampleRate = 44100; - if ((rc = AOpenAudio(&info)) != AUDIO_ERROR_NONE) - { - CHAR szText[80]; - AGetErrorText(rc, szText, sizeof(szText) - 1); - fprintf(log_get(),"audio: error: %s\n", szText); - fflush(log_get()); - return(0); - } + if ((rc = AOpenAudio(&info)) != AUDIO_ERROR_NONE) + { + CHAR szText[80]; + AGetErrorText(rc, szText, sizeof(szText) - 1); + fprintf(log_get(),"audio: error: %s\n", szText); + fflush(log_get()); + return(0); + } - // open 6 voices ( 4 pcm, one noise, and one sweep) - AOpenVoices(6); + // open 6 voices ( 4 pcm, one noise, and one sweep) + AOpenVoices(6); - // create the 4 pcm channels - for (i=0; i<4; i++) - { - // create the channel - ACreateAudioVoice(&ws_audio_pcm_voice[i]); - ASetVoiceVolume ( ws_audio_pcm_voice[i], AUDIO_MAX_VOLUME); - ASetVoicePanning ( ws_audio_pcm_voice[i], AUDIO_MIN_PANNING); + // create the 4 pcm channels + for (i=0; i<4; i++) + { + // create the channel + ACreateAudioVoice(&ws_audio_pcm_voice[i]); + ASetVoiceVolume ( ws_audio_pcm_voice[i], AUDIO_MAX_VOLUME); + ASetVoicePanning ( ws_audio_pcm_voice[i], AUDIO_MIN_PANNING); - // create a looped sound buffer - ws_audio_pcm_wave[i].nSampleRate = info.nSampleRate; - ws_audio_pcm_wave[i].dwLength = BUFSIZE; - ws_audio_pcm_wave[i].dwLoopStart = 0; - ws_audio_pcm_wave[i].dwLoopEnd = ws_audio_pcm_wave[i].dwLength; - ws_audio_pcm_wave[i].wFormat = AUDIO_FORMAT_8BITS | AUDIO_FORMAT_MONO | AUDIO_FORMAT_LOOP; + // create a looped sound buffer + ws_audio_pcm_wave[i].nSampleRate = info.nSampleRate; + ws_audio_pcm_wave[i].dwLength = BUFSIZE; + ws_audio_pcm_wave[i].dwLoopStart = 0; + ws_audio_pcm_wave[i].dwLoopEnd = ws_audio_pcm_wave[i].dwLength; + ws_audio_pcm_wave[i].wFormat = AUDIO_FORMAT_8BITS | AUDIO_FORMAT_MONO | AUDIO_FORMAT_LOOP; - ACreateAudioData(&ws_audio_pcm_wave[i]); + ACreateAudioData(&ws_audio_pcm_wave[i]); - // channel is not playing yet - ws_audio_channel_isPlaying[i]=0; + // channel is not playing yet + ws_audio_channel_isPlaying[i]=0; - // clear the channel - ws_audio_clear_channel(i); - } + // clear the channel + ws_audio_clear_channel(i); + } - // create the noise channel - { - // create the channel - ACreateAudioVoice(&ws_audio_noise_voice); - ASetVoiceVolume ( ws_audio_noise_voice, AUDIO_MAX_VOLUME); - ASetVoicePanning ( ws_audio_noise_voice, AUDIO_MAX_PANNING>>1); + // create the noise channel + { + // create the channel + ACreateAudioVoice(&ws_audio_noise_voice); + ASetVoiceVolume ( ws_audio_noise_voice, AUDIO_MAX_VOLUME); + ASetVoicePanning ( ws_audio_noise_voice, AUDIO_MAX_PANNING>>1); - // create a looped sound buffer - ws_audio_noise_wave.nSampleRate = info.nSampleRate; - ws_audio_noise_wave.dwLength = (BUFSIZEP<<4); - ws_audio_noise_wave.dwLoopStart = 0; - ws_audio_noise_wave.dwLoopEnd = ws_audio_noise_wave.dwLength; - ws_audio_noise_wave.wFormat = AUDIO_FORMAT_8BITS | AUDIO_FORMAT_MONO | AUDIO_FORMAT_LOOP; + // create a looped sound buffer + ws_audio_noise_wave.nSampleRate = info.nSampleRate; + ws_audio_noise_wave.dwLength = (BUFSIZEP<<4); + ws_audio_noise_wave.dwLoopStart = 0; + ws_audio_noise_wave.dwLoopEnd = ws_audio_noise_wave.dwLength; + ws_audio_noise_wave.wFormat = AUDIO_FORMAT_8BITS | AUDIO_FORMAT_MONO | AUDIO_FORMAT_LOOP; - ACreateAudioData(&ws_audio_noise_wave); + ACreateAudioData(&ws_audio_noise_wave); - // channel is not playing yet - ws_audio_channel_isPlaying[4]=0; + // channel is not playing yet + ws_audio_channel_isPlaying[4]=0; - // clear the channel - ws_audio_clear_channel(4); - } + // clear the channel + ws_audio_clear_channel(4); + } - // create the sweep channel - { - // create the channel - ACreateAudioVoice(&ws_audio_sweep_voice); - ASetVoiceVolume ( ws_audio_sweep_voice, AUDIO_MAX_VOLUME); - ASetVoicePanning ( ws_audio_sweep_voice, AUDIO_MAX_PANNING); + // create the sweep channel + { + // create the channel + ACreateAudioVoice(&ws_audio_sweep_voice); + ASetVoiceVolume ( ws_audio_sweep_voice, AUDIO_MAX_VOLUME); + ASetVoicePanning ( ws_audio_sweep_voice, AUDIO_MAX_PANNING); - // create a looped sound buffer - ws_audio_sweep_wave.nSampleRate = info.nSampleRate; - ws_audio_sweep_wave.dwLength = BUFSIZEN; - ws_audio_sweep_wave.dwLoopStart = 0; - ws_audio_sweep_wave.dwLoopEnd = ws_audio_sweep_wave.dwLength; - ws_audio_sweep_wave.wFormat = AUDIO_FORMAT_8BITS | AUDIO_FORMAT_MONO | AUDIO_FORMAT_LOOP; + // create a looped sound buffer + ws_audio_sweep_wave.nSampleRate = info.nSampleRate; + ws_audio_sweep_wave.dwLength = BUFSIZEN; + ws_audio_sweep_wave.dwLoopStart = 0; + ws_audio_sweep_wave.dwLoopEnd = ws_audio_sweep_wave.dwLength; + ws_audio_sweep_wave.wFormat = AUDIO_FORMAT_8BITS | AUDIO_FORMAT_MONO | AUDIO_FORMAT_LOOP; - ACreateAudioData(&ws_audio_sweep_wave); + ACreateAudioData(&ws_audio_sweep_wave); - // channel is not playing yet - ws_audio_channel_isPlaying[5]=0; + // channel is not playing yet + ws_audio_channel_isPlaying[5]=0; - // clear the channel - ws_audio_clear_channel(5); - } + // clear the channel + ws_audio_clear_channel(5); + } - // initialize the noise channel data - int rand; + // initialize the noise channel data + int rand; - for(i=0; i<8; i++) - { - for(j=0; jBPSMAX) - { - Freq=BPSMAX; - } + if(Freq>BPSMAX) + { + Freq=BPSMAX; + } - else if(FreqRight) - { - ws_audio_set_channel_volume(Channel,Left); - } - else - { - ws_audio_set_channel_volume(Channel,Right); - } + if(Left>Right) + { + ws_audio_set_channel_volume(Channel,Left); + } + else + { + ws_audio_set_channel_volume(Channel,Right); + } - pan=TblPan[Left][Right]; + pan=TblPan[Left][Right]; - if(ChCurPan[Channel]==pan) - { - return; - } + if(ChCurPan[Channel]==pan) + { + return; + } - ChCurPan[Channel]=pan; + ChCurPan[Channel]=pan; - if (pan>10000) - { - pan=10000; - } + if (pan>10000) + { + pan=10000; + } - pan=((pan+10000)*AUDIO_MAX_PANNING)/20000; + pan=((pan+10000)*AUDIO_MAX_PANNING)/20000; - if (Channel==5) - { - ASetVoicePanning(ws_audio_sweep_voice,pan); - } - else if (Channel==4) - { - ASetVoicePanning(ws_audio_noise_voice,pan); - } - else - { - ASetVoicePanning(ws_audio_pcm_voice[Channel],pan); - } + if (Channel==5) + { + ASetVoicePanning(ws_audio_sweep_voice,pan); + } + else if (Channel==4) + { + ASetVoicePanning(ws_audio_noise_voice,pan); + } + else + { + ASetVoicePanning(ws_audio_pcm_voice[Channel],pan); + } #endif } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -1025,32 +1042,33 @@ void ws_audio_set_channel_pan(int Channel,int Left,int Right) // // //////////////////////////////////////////////////////////////////////////////// -void ws_audio_set_channel_pdata(int Channel,int Index) +void ws_audio_set_channel_pdata(int Channel, int Index) { #if 0 - unsigned char *pData; + unsigned char *pData; - if(Channel==5) - { - pData=PDataN[Index]; + if(Channel==5) + { + pData=PDataN[Index]; - memcpy(ws_audio_sweep_wave.lpData, pData, ws_audio_sweep_wave.dwLength); - AWriteAudioData(&ws_audio_sweep_wave, 0, ws_audio_sweep_wave.dwLength); - } - else if(Channel==4) - { - pData=PDataP; - memcpy(ws_audio_noise_wave.lpData, pData, ws_audio_noise_wave.dwLength); - AWriteAudioData(&ws_audio_noise_wave, 0, ws_audio_noise_wave.dwLength); - } - else - { - pData=PData[Channel]; - memcpy(ws_audio_pcm_wave[Channel].lpData, pData, ws_audio_pcm_wave[Channel].dwLength); - AWriteAudioData(&ws_audio_pcm_wave[Channel], 0, ws_audio_pcm_wave[Channel].dwLength); - } + memcpy(ws_audio_sweep_wave.lpData, pData, ws_audio_sweep_wave.dwLength); + AWriteAudioData(&ws_audio_sweep_wave, 0, ws_audio_sweep_wave.dwLength); + } + else if(Channel==4) + { + pData=PDataP; + memcpy(ws_audio_noise_wave.lpData, pData, ws_audio_noise_wave.dwLength); + AWriteAudioData(&ws_audio_noise_wave, 0, ws_audio_noise_wave.dwLength); + } + else + { + pData=PData[Channel]; + memcpy(ws_audio_pcm_wave[Channel].lpData, pData, ws_audio_pcm_wave[Channel].dwLength); + AWriteAudioData(&ws_audio_pcm_wave[Channel], 0, ws_audio_pcm_wave[Channel].dwLength); + } #endif } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -1062,25 +1080,26 @@ void ws_audio_set_channel_pdata(int Channel,int Index) // // //////////////////////////////////////////////////////////////////////////////// -void ws_audio_set_channels_pbuf(int Addr,int Data) +void ws_audio_set_channels_pbuf(int Addr, int Data) { #if 0 - int i,j; + int i,j; - i=(Addr&0x30)>>4; + i=(Addr&0x30)>>4; - for(j=(Addr&0x0F)<<1; j>4)&0x0f)*17-128; - } + for(j=(Addr&0x0F)<<1; j>4)&0x0f)*17-128; + } - if((Addr&0x0F)==0x0F) - { - ws_audio_set_channel_pdata(i,0); - } + if((Addr&0x0F)==0x0F) + { + ws_audio_set_channel_pdata(i,0); + } #endif } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -1095,13 +1114,14 @@ void ws_audio_set_channels_pbuf(int Addr,int Data) void ws_audio_rst_channel(int Channel) { #if 0 - if(Channel==2) - { - ws_audio_set_channel_frequency(2,ChPerInit); - SwpCurPeriod=ChPerInit; - } + if(Channel==2) + { + ws_audio_set_channel_frequency(2,ChPerInit); + SwpCurPeriod=ChPerInit; + } #endif } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -1116,45 +1136,45 @@ void ws_audio_rst_channel(int Channel) int ws_audio_int(void) { #if 0 - unsigned int value; - static int i; + unsigned int value; + static int i; - if((SwpStep)&&(SNDMOD&0x40)) - { - if(CntSwp<0) - { - CntSwp=SwpTime; - SwpCurPeriod+=SwpStep; - SwpCurPeriod&=0x7FF; - value=3072000/(2048-SwpCurPeriod); + if((SwpStep)&&(SNDMOD&0x40)) + { + if(CntSwp<0) + { + CntSwp=SwpTime; + SwpCurPeriod+=SwpStep; + SwpCurPeriod&=0x7FF; + value=3072000/(2048-SwpCurPeriod); - if(value>100000) - { - value=100000; - ws_audio_set_channel_volume(2,0); - } + if(value>100000) + { + value=100000; + ws_audio_set_channel_volume(2,0); + } - if(value<100) - { - value=100; - } + if(value<100) + { + value=100; + } - ASetVoiceFrequency(ws_audio_pcm_voice[2],value); - } + ASetVoiceFrequency(ws_audio_pcm_voice[2],value); + } - CntSwp--; - } + CntSwp--; + } - i++; + i++; - if(i>=BUFSIZEN) - { - i=0; - } + if(i>=BUFSIZEN) + { + i=0; + } - return RandData[i]; + return RandData[i]; #endif - return 1; + return 1; } //////////////////////////////////////////////////////////////////////////////// // @@ -1175,31 +1195,32 @@ uint32_t TickZ=0,PcmCount; void ws_audio_set_pcm(int Data) { #if 0 - uint32_t tick; - PDataP[PCMPos++]=(unsigned char)(Data+128); - tick=SDL_GetTicks(); - PcmCount++; + uint32_t tick; + PDataP[PCMPos++]=(unsigned char)(Data+128); + tick=SDL_GetTicks(); + PcmCount++; - if(tick>=TickZ) - { - TickZ=tick+125; - PcmCount<<=3; + if(tick>=TickZ) + { + TickZ=tick+125; + PcmCount<<=3; - if(PcmCount>=10000) - { - PcmCount=12000; - } + if(PcmCount>=10000) + { + PcmCount=12000; + } - ASetVoiceFrequency(ws_audio_noise_voice,PcmCount); - PcmCount=0; - } + ASetVoiceFrequency(ws_audio_noise_voice,PcmCount); + PcmCount=0; + } - if(PCMPos>=BUFSIZEP) - { - ws_audio_flash_pcm(); - } + if(PCMPos>=BUFSIZEP) + { + ws_audio_flash_pcm(); + } #endif } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -1214,33 +1235,34 @@ void ws_audio_set_pcm(int Data) void ws_audio_flash_pcm(void) { #if 0 - uint32_t len1; + uint32_t len1; - const uint32_t WrPos[16]= - { - BUFSIZEP*0,BUFSIZEP*1,BUFSIZEP*2,BUFSIZEP*3, - BUFSIZEP*4,BUFSIZEP*5,BUFSIZEP*6,BUFSIZEP*7, - BUFSIZEP*8,BUFSIZEP*9,BUFSIZEP*10,BUFSIZEP*11, - BUFSIZEP*12,BUFSIZEP*13,BUFSIZEP*14,BUFSIZEP*15, - }; + const uint32_t WrPos[16]= + { + BUFSIZEP*0,BUFSIZEP*1,BUFSIZEP*2,BUFSIZEP*3, + BUFSIZEP*4,BUFSIZEP*5,BUFSIZEP*6,BUFSIZEP*7, + BUFSIZEP*8,BUFSIZEP*9,BUFSIZEP*10,BUFSIZEP*11, + BUFSIZEP*12,BUFSIZEP*13,BUFSIZEP*14,BUFSIZEP*15, + }; - len1=BUFSIZEP; + len1=BUFSIZEP; - if (ws_audio_noise_wave.lpData == NULL) - { - return; - } + if (ws_audio_noise_wave.lpData == NULL) + { + return; + } - memcpy(&ws_audio_noise_wave.lpData[WrPos[PcmWrPos]], PDataP, len1); - AWriteAudioData(&ws_audio_noise_wave, 0, ws_audio_noise_wave.dwLength); + memcpy(&ws_audio_noise_wave.lpData[WrPos[PcmWrPos]], PDataP, len1); + AWriteAudioData(&ws_audio_noise_wave, 0, ws_audio_noise_wave.dwLength); - PcmWrPos++; - PcmWrPos&=0xF; - memset(PDataP,PL,sizeof(PDataP)); - PCMPos=0; + PcmWrPos++; + PcmWrPos&=0xF; + memset(PDataP,PL,sizeof(PDataP)); + PCMPos=0; #endif } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -1254,12 +1276,13 @@ void ws_audio_flash_pcm(void) //////////////////////////////////////////////////////////////////////////////// void ws_audio_write_byte(uint32_t offset, uint8_t value) { - if (!((offset-WaveMap)&0xFFC0)) - { - ws_audio_set_channels_pbuf(offset&0x003F,value); - internalRam[offset]=value; - } + if (!((offset - WaveMap) & 0xFFC0)) + { + ws_audio_set_channels_pbuf(offset & 0x003F, value); + internalRam[offset] = value; + } } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -1273,66 +1296,67 @@ void ws_audio_write_byte(uint32_t offset, uint8_t value) //////////////////////////////////////////////////////////////////////////////// void ws_audio_process(void) { - uint32_t i, j, b; - i=ws_audio_int(); - PCSRL=(uint8_t)(i&0xFF); - PCSRH=(uint8_t)((i>>8)&0xFF); + uint32_t i, j, b; + i = ws_audio_int(); + PCSRL = (uint8_t)(i & 0xFF); + PCSRH = (uint8_t)((i >> 8) & 0xFF); - if((SDMACTL&0x88)==0x80) - { - i=(SDMACH<<8)|SDMACL; - j=(SDMASB<<16)|(SDMASH<<8)|SDMASL; - b=cpu_readmem20(j); + if ((SDMACTL & 0x88) == 0x80) + { + i = (SDMACH << 8) | SDMACL; + j = (SDMASB << 16) | (SDMASH << 8) | SDMASL; + b = cpu_readmem20(j); - if(!ws_audio_channel_isPlaying[5]) - { - b=0x80; - } + if (!ws_audio_channel_isPlaying[5]) + { + b = 0x80; + } - ws_ioRam[0x89]=b; - i--; - j++; + ws_ioRam[0x89] = b; + i--; + j++; - if(i<32) - { - i=0; - SDMACTL&=0x7F; - } + if (i < 32) + { + i = 0; + SDMACTL &= 0x7F; + } - SDMASB=(uint8_t)((j>>16)&0xFF); - SDMASH=(uint8_t)((j>>8)&0xFF); - SDMASL=(uint8_t)(j&0xFF); - SDMACH=(uint8_t)((i>>8)&0xFF); - SDMACL=(uint8_t)(i&0xFF); - } - else if((SNDMOD&0x22)==0x22) - { - b=ws_ioRam[0x89]; + SDMASB = (uint8_t)((j >> 16) & 0xFF); + SDMASH = (uint8_t)((j >> 8) & 0xFF); + SDMASL = (uint8_t)(j & 0xFF); + SDMACH = (uint8_t)((i >> 8) & 0xFF); + SDMACL = (uint8_t)(i & 0xFF); + } + else if ((SNDMOD & 0x22) == 0x22) + { + b = ws_ioRam[0x89]; - if(!ws_audio_channel_isPlaying[4]) - { - b=0x80; - } - } - else - { - b=0x80; - } + if (!ws_audio_channel_isPlaying[4]) + { + b = 0x80; + } + } + else + { + b = 0x80; + } - b>>=1; - b+=0x40; + b >>= 1; + b += 0x40; - if(b>0xAA) - { - b=0xAA; - } - else if(b<0x55) - { - b=0x55; - } + if (b > 0xAA) + { + b = 0xAA; + } + else if (b < 0x55) + { + b = 0x55; + } - ws_audio_set_pcm(b); + ws_audio_set_pcm(b); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -1347,92 +1371,93 @@ void ws_audio_process(void) void ws_audio_readState(int fp) { #if 0 - long lpdwPosition; - long lpdwFrequency; - unsigned int lpnVolume; - unsigned int lpnPanning; - int lpnStatus; - unsigned char *pData; + long lpdwPosition; + long lpdwFrequency; + unsigned int lpnVolume; + unsigned int lpnPanning; + int lpnStatus; + unsigned char *pData; - read(fp,&PCMPos,sizeof(uint32_t)); - read(fp,&TickZ,sizeof(uint32_t)); - read(fp,&PcmCount,sizeof(uint32_t)); - read(fp,&WaveMap,sizeof(int)); - read(fp,&ChPerInit,sizeof(int)); - read(fp,&SwpTime,sizeof(int)); - read(fp,&SwpStep,sizeof(int)); - read(fp,&SwpCurPeriod,sizeof(int)); - read(fp,&MainVol,sizeof(int)); - read(fp,&CntSwp,sizeof(int)); - read(fp,&PcmWrPos,sizeof(int)); + read(fp,&PCMPos,sizeof(uint32_t)); + read(fp,&TickZ,sizeof(uint32_t)); + read(fp,&PcmCount,sizeof(uint32_t)); + read(fp,&WaveMap,sizeof(int)); + read(fp,&ChPerInit,sizeof(int)); + read(fp,&SwpTime,sizeof(int)); + read(fp,&SwpStep,sizeof(int)); + read(fp,&SwpCurPeriod,sizeof(int)); + read(fp,&MainVol,sizeof(int)); + read(fp,&CntSwp,sizeof(int)); + read(fp,&PcmWrPos,sizeof(int)); - read(fp,ws_audio_channel_isPlaying,sizeof(uint32_t)*6); + read(fp,ws_audio_channel_isPlaying,sizeof(uint32_t)*6); - read(fp,PData,sizeof(unsigned char)*4*BUFSIZE); - read(fp,PDataP,sizeof(unsigned char)*(BUFSIZEP<<4)); - read(fp,PDataN,sizeof(unsigned char)*8*BUFSIZEN); - read(fp,PDataN,sizeof(int)*BUFSIZEN); + read(fp,PData,sizeof(unsigned char)*4*BUFSIZE); + read(fp,PDataP,sizeof(unsigned char)*(BUFSIZEP<<4)); + read(fp,PDataN,sizeof(unsigned char)*8*BUFSIZEN); + read(fp,PDataN,sizeof(int)*BUFSIZEN); - for (int i=0; i<4; i++) - { - read(fp,&lpdwPosition,sizeof(long)); - read(fp,&lpdwFrequency,sizeof(long)); - read(fp,&lpnVolume,sizeof(unsigned int)); - read(fp,&lpnPanning,sizeof(unsigned int)); - read(fp,&lpnStatus,sizeof(int)); - ASetVoicePosition(ws_audio_pcm_voice[i],lpdwPosition); - ASetVoiceFrequency(ws_audio_pcm_voice[i], lpdwFrequency); - ASetVoiceVolume(ws_audio_pcm_voice[i], lpnVolume); - ASetVoicePanning(ws_audio_pcm_voice[i], lpnPanning); - pData=PData[i]; - memcpy(ws_audio_pcm_wave[i].lpData, pData, ws_audio_pcm_wave[i].dwLength); - AWriteAudioData(&ws_audio_pcm_wave[i], 0, ws_audio_pcm_wave[i].dwLength); + for (int i=0; i<4; i++) + { + read(fp,&lpdwPosition,sizeof(long)); + read(fp,&lpdwFrequency,sizeof(long)); + read(fp,&lpnVolume,sizeof(unsigned int)); + read(fp,&lpnPanning,sizeof(unsigned int)); + read(fp,&lpnStatus,sizeof(int)); + ASetVoicePosition(ws_audio_pcm_voice[i],lpdwPosition); + ASetVoiceFrequency(ws_audio_pcm_voice[i], lpdwFrequency); + ASetVoiceVolume(ws_audio_pcm_voice[i], lpnVolume); + ASetVoicePanning(ws_audio_pcm_voice[i], lpnPanning); + pData=PData[i]; + memcpy(ws_audio_pcm_wave[i].lpData, pData, ws_audio_pcm_wave[i].dwLength); + AWriteAudioData(&ws_audio_pcm_wave[i], 0, ws_audio_pcm_wave[i].dwLength); - if (ws_audio_channel_isPlaying[i]) - { - APlayVoice(ws_audio_pcm_voice[i], &ws_audio_pcm_wave[i]); - } - } + if (ws_audio_channel_isPlaying[i]) + { + APlayVoice(ws_audio_pcm_voice[i], &ws_audio_pcm_wave[i]); + } + } - ASetVoicePosition(ws_audio_noise_voice,lpdwPosition); - ASetVoiceFrequency(ws_audio_noise_voice, lpdwFrequency); - ASetVoiceVolume(ws_audio_noise_voice, lpnVolume); - ASetVoicePanning(ws_audio_noise_voice, lpnPanning); - pData=PDataP; - memcpy(ws_audio_noise_wave.lpData, pData, ws_audio_noise_wave.dwLength); - AWriteAudioData(&ws_audio_noise_wave, 0, ws_audio_noise_wave.dwLength); + ASetVoicePosition(ws_audio_noise_voice,lpdwPosition); + ASetVoiceFrequency(ws_audio_noise_voice, lpdwFrequency); + ASetVoiceVolume(ws_audio_noise_voice, lpnVolume); + ASetVoicePanning(ws_audio_noise_voice, lpnPanning); + pData=PDataP; + memcpy(ws_audio_noise_wave.lpData, pData, ws_audio_noise_wave.dwLength); + AWriteAudioData(&ws_audio_noise_wave, 0, ws_audio_noise_wave.dwLength); - if (ws_audio_channel_isPlaying[4]) - { - APlayVoice(ws_audio_noise_voice, &ws_audio_noise_wave); - } + if (ws_audio_channel_isPlaying[4]) + { + APlayVoice(ws_audio_noise_voice, &ws_audio_noise_wave); + } - read(fp,&lpdwPosition,sizeof(long)); - read(fp,&lpdwFrequency,sizeof(long)); - read(fp,&lpnVolume,sizeof(unsigned int)); - read(fp,&lpnPanning,sizeof(unsigned int)); - read(fp,&lpnStatus,sizeof(int)); + read(fp,&lpdwPosition,sizeof(long)); + read(fp,&lpdwFrequency,sizeof(long)); + read(fp,&lpnVolume,sizeof(unsigned int)); + read(fp,&lpnPanning,sizeof(unsigned int)); + read(fp,&lpnStatus,sizeof(int)); - ASetVoicePosition(ws_audio_sweep_voice,lpdwPosition); - ASetVoiceFrequency(ws_audio_sweep_voice, lpdwFrequency); - ASetVoiceVolume(ws_audio_sweep_voice, lpnVolume); - ASetVoicePanning(ws_audio_sweep_voice, lpnPanning); - pData=PDataN[0]; - memcpy(ws_audio_sweep_wave.lpData, pData, ws_audio_sweep_wave.dwLength); - AWriteAudioData(&ws_audio_sweep_wave, 0, ws_audio_sweep_wave.dwLength); + ASetVoicePosition(ws_audio_sweep_voice,lpdwPosition); + ASetVoiceFrequency(ws_audio_sweep_voice, lpdwFrequency); + ASetVoiceVolume(ws_audio_sweep_voice, lpnVolume); + ASetVoicePanning(ws_audio_sweep_voice, lpnPanning); + pData=PDataN[0]; + memcpy(ws_audio_sweep_wave.lpData, pData, ws_audio_sweep_wave.dwLength); + AWriteAudioData(&ws_audio_sweep_wave, 0, ws_audio_sweep_wave.dwLength); - if (ws_audio_channel_isPlaying[5]) - { - APlayVoice(ws_audio_sweep_voice, &ws_audio_sweep_wave); - } + if (ws_audio_channel_isPlaying[5]) + { + APlayVoice(ws_audio_sweep_voice, &ws_audio_sweep_wave); + } - read(fp,&lpdwPosition,sizeof(long)); - read(fp,&lpdwFrequency,sizeof(long)); - read(fp,&lpnVolume,sizeof(unsigned int)); - read(fp,&lpnPanning,sizeof(unsigned int)); - read(fp,&lpnStatus,sizeof(int)); + read(fp,&lpdwPosition,sizeof(long)); + read(fp,&lpdwFrequency,sizeof(long)); + read(fp,&lpnVolume,sizeof(unsigned int)); + read(fp,&lpnPanning,sizeof(unsigned int)); + read(fp,&lpnStatus,sizeof(int)); #endif } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -1447,68 +1472,68 @@ void ws_audio_readState(int fp) void ws_audio_writeState(int fp) { #if 0 - int32_t lpdwPosition; - int32_t lpdwFrequency; - uint16_t lpnVolume; - uint16_t lpnPanning; - int8_t lpnStatus; + int32_t lpdwPosition; + int32_t lpdwFrequency; + uint16_t lpnVolume; + uint16_t lpnPanning; + int8_t lpnStatus; - write(fp,&PCMPos,sizeof(uint32_t)); - write(fp,&TickZ,sizeof(uint32_t)); - write(fp,&PcmCount,sizeof(uint32_t)); - write(fp,&WaveMap,sizeof(int)); - write(fp,&ChPerInit,sizeof(int)); - write(fp,&SwpTime,sizeof(int)); - write(fp,&SwpStep,sizeof(int)); - write(fp,&SwpCurPeriod,sizeof(int)); - write(fp,&MainVol,sizeof(int)); - write(fp,&CntSwp,sizeof(int)); - write(fp,&PcmWrPos,sizeof(int)); + write(fp,&PCMPos,sizeof(uint32_t)); + write(fp,&TickZ,sizeof(uint32_t)); + write(fp,&PcmCount,sizeof(uint32_t)); + write(fp,&WaveMap,sizeof(int)); + write(fp,&ChPerInit,sizeof(int)); + write(fp,&SwpTime,sizeof(int)); + write(fp,&SwpStep,sizeof(int)); + write(fp,&SwpCurPeriod,sizeof(int)); + write(fp,&MainVol,sizeof(int)); + write(fp,&CntSwp,sizeof(int)); + write(fp,&PcmWrPos,sizeof(int)); - write(fp,ws_audio_channel_isPlaying,sizeof(uint32_t)*6); + write(fp,ws_audio_channel_isPlaying,sizeof(uint32_t)*6); - write(fp,PData,sizeof(unsigned char)*4*BUFSIZE); - write(fp,PDataP,sizeof(unsigned char)*(BUFSIZEP<<4)); - write(fp,PDataN,sizeof(unsigned char)*8*BUFSIZEN); - write(fp,PDataN,sizeof(int)*BUFSIZEN); + write(fp,PData,sizeof(unsigned char)*4*BUFSIZE); + write(fp,PDataP,sizeof(unsigned char)*(BUFSIZEP<<4)); + write(fp,PDataN,sizeof(unsigned char)*8*BUFSIZEN); + write(fp,PDataN,sizeof(int)*BUFSIZEN); - for (int i=0; i<4; i++) - { - AGetVoicePosition(ws_audio_pcm_voice[i],&lpdwPosition); - AGetVoiceFrequency(ws_audio_pcm_voice[i], &lpdwFrequency); - AGetVoiceVolume(ws_audio_pcm_voice[i], &lpnVolume); - AGetVoicePanning(ws_audio_pcm_voice[i], &lpnPanning); - AGetVoiceStatus(ws_audio_pcm_voice[i], &lpnStatus); + for (int i=0; i<4; i++) + { + AGetVoicePosition(ws_audio_pcm_voice[i],&lpdwPosition); + AGetVoiceFrequency(ws_audio_pcm_voice[i], &lpdwFrequency); + AGetVoiceVolume(ws_audio_pcm_voice[i], &lpnVolume); + AGetVoicePanning(ws_audio_pcm_voice[i], &lpnPanning); + AGetVoiceStatus(ws_audio_pcm_voice[i], &lpnStatus); - write(fp,&lpdwPosition,sizeof(long)); - write(fp,&lpdwFrequency,sizeof(long)); - write(fp,&lpnVolume,sizeof(unsigned int)); - write(fp,&lpnPanning,sizeof(unsigned int)); - write(fp,&lpnStatus,sizeof(int)); - } + write(fp,&lpdwPosition,sizeof(long)); + write(fp,&lpdwFrequency,sizeof(long)); + write(fp,&lpnVolume,sizeof(unsigned int)); + write(fp,&lpnPanning,sizeof(unsigned int)); + write(fp,&lpnStatus,sizeof(int)); + } - AGetVoicePosition(ws_audio_noise_voice,&lpdwPosition); - AGetVoiceFrequency(ws_audio_noise_voice, &lpdwFrequency); - AGetVoiceVolume(ws_audio_noise_voice, &lpnVolume); - AGetVoicePanning(ws_audio_noise_voice, &lpnPanning); - AGetVoiceStatus(ws_audio_noise_voice, &lpnStatus); + AGetVoicePosition(ws_audio_noise_voice,&lpdwPosition); + AGetVoiceFrequency(ws_audio_noise_voice, &lpdwFrequency); + AGetVoiceVolume(ws_audio_noise_voice, &lpnVolume); + AGetVoicePanning(ws_audio_noise_voice, &lpnPanning); + AGetVoiceStatus(ws_audio_noise_voice, &lpnStatus); - write(fp,&lpdwPosition,sizeof(long)); - write(fp,&lpdwFrequency,sizeof(long)); - write(fp,&lpnVolume,sizeof(unsigned int)); - write(fp,&lpnPanning,sizeof(unsigned int)); - write(fp,&lpnStatus,sizeof(int)); + write(fp,&lpdwPosition,sizeof(long)); + write(fp,&lpdwFrequency,sizeof(long)); + write(fp,&lpnVolume,sizeof(unsigned int)); + write(fp,&lpnPanning,sizeof(unsigned int)); + write(fp,&lpnStatus,sizeof(int)); - AGetVoicePosition(ws_audio_sweep_voice,&lpdwPosition); - AGetVoiceFrequency(ws_audio_sweep_voice, &lpdwFrequency); - AGetVoiceVolume(ws_audio_sweep_voice, &lpnVolume); - AGetVoicePanning(ws_audio_sweep_voice, &lpnPanning); - AGetVoiceStatus(ws_audio_sweep_voice, &lpnStatus); + AGetVoicePosition(ws_audio_sweep_voice,&lpdwPosition); + AGetVoiceFrequency(ws_audio_sweep_voice, &lpdwFrequency); + AGetVoiceVolume(ws_audio_sweep_voice, &lpnVolume); + AGetVoicePanning(ws_audio_sweep_voice, &lpnPanning); + AGetVoiceStatus(ws_audio_sweep_voice, &lpnStatus); - write(fp,&lpdwPosition,sizeof(long)); - write(fp,&lpdwFrequency,sizeof(long)); - write(fp,&lpnVolume,sizeof(unsigned int)); - write(fp,&lpnPanning,sizeof(unsigned int)); - write(fp,&lpnStatus,sizeof(int)); + write(fp,&lpdwPosition,sizeof(long)); + write(fp,&lpdwFrequency,sizeof(long)); + write(fp,&lpnVolume,sizeof(unsigned int)); + write(fp,&lpnPanning,sizeof(unsigned int)); + write(fp,&lpnStatus,sizeof(int)); #endif } diff --git a/source/audio.h b/source/audio.h index ae73167..00da838 100644 --- a/source/audio.h +++ b/source/audio.h @@ -1,5 +1,10 @@ -////////////////////////////////////////////////////////////////////////////// -// +/* + * NewOswan + * audio.h: + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ ////////////////////////////////////////////////////////////////////////////// // // diff --git a/source/emulate.c b/source/emulate.c index 9557a79..d12f45c 100644 --- a/source/emulate.c +++ b/source/emulate.c @@ -1,6 +1,11 @@ -//////////////////////////////////////////////////////////////////////////////// -// Main emulation loop -//////////////////////////////////////////////////////////////////////////////// +/* + * NewOswan + * emulate.c: + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ +////////////////////////////////////////////////////////////////////////////// // // // @@ -23,6 +28,7 @@ #define GLFW_INCLUDE_GLEXT #define GL_SILENCE_DEPRECATION + #include /* "Apple" fix */ #ifndef GL_TEXTURE_RECTANGLE @@ -39,12 +45,8 @@ #include "audio.h" #include "memory.h" -char app_window_title[256]; -int app_gameRunning=0; -int app_terminate=0; -int app_fullscreen=0; -int app_rotated=0; - +char app_window_title[256]; +int app_terminate = 0; int ws_key_esc = 0; @@ -68,13 +70,13 @@ struct GLWindow_t }; static GLWindow mainWindow; static int window_num = 0; + static void ShowScreen(GLWindow *g, int w, int h) { glBindTexture(GL_TEXTURE_RECTANGLE, g->videoTexture); // glTexSubImage2D is faster when not using a texture range - glTexSubImage2D(GL_TEXTURE_RECTANGLE, 0, 0, 0, w, h, - GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, g->videoMemory); + glTexSubImage2D(GL_TEXTURE_RECTANGLE, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, g->videoMemory); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); @@ -92,12 +94,14 @@ static void ShowScreen(GLWindow *g, int w, int h) glFlush(); } + static void GLWindowInitEx(GLWindow *g, int w, int h) { g->WIDTH = w; g->HEIGHT = h; g->videoTexture = window_num++; } + static void setupGL(GLWindow *g, int w, int h) { g->videoMemory = (uint8_t *)malloc(w * h * sizeof(uint16_t)); @@ -120,10 +124,6 @@ static void setupGL(GLWindow *g, int w, int h) glEnable(GL_TEXTURE_RECTANGLE); glBindTexture(GL_TEXTURE_RECTANGLE, g->videoTexture); - // glTextureRangeAPPLE(GL_TEXTURE_RECTANGLE_NV_EXT, 0, NULL); - - // glTexParameteri(GL_TEXTURE_RECTANGLE_NV_EXT, GL_TEXTURE_STORAGE_HINT_APPLE , GL_STORAGE_CACHED_APPLE); - // glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE); glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); @@ -134,6 +134,7 @@ static void setupGL(GLWindow *g, int w, int h) glDisable(GL_DEPTH_TEST); } + void restoreGL(GLWindow *g) { //Tell OpenGL how to convert from coordinates to pixel values @@ -154,6 +155,7 @@ void restoreGL(GLWindow *g) glEnable(GL_TEXTURE_RECTANGLE); glDisable(GL_DEPTH_TEST); } + static void kbHandler(GLFWwindow *window, int key, int scan, int action, int mod) { struct KeyArray *keyArray; @@ -174,16 +176,19 @@ static void kbHandler(GLFWwindow *window, int key, int scan, int action, int mod /*printf("key:%d, state:%d debounce:%d, laststate:%d\n", key, keyArray[key].curState, keyArray[key].debounced, keyArray[key].lastState);*/ } + static void sizeHandler(GLFWwindow *window, int xs, int ys) { glfwMakeContextCurrent(window); glViewport(0, 0, xs, ys); ShowScreen(&mainWindow, 244, 144); } + static void error_callback(int error, const char *description) { puts(description); } + static void initDisplay(GLWindow *g) { int h = g->HEIGHT; @@ -216,10 +221,12 @@ static void initDisplay(GLWindow *g) glfwSetKeyCallback(g->windows, kbHandler); glfwSetWindowSizeCallback(g->windows, sizeHandler); } + static void clearScreen(GLWindow *g) { memset(g->videoMemory, 0, sizeof(uint8_t) * g->WIDTH * g->HEIGHT * 4); } + static void updateScreen(GLWindow *g) { /* Update windows code */ @@ -228,6 +235,7 @@ static void updateScreen(GLWindow *g) glfwSwapBuffers(g->windows); glfwPollEvents(); } + uint64_t getTicks() { struct timeval curTime; @@ -235,7 +243,7 @@ uint64_t getTicks() /* Get datetime */ gettimeofday(&curTime, NULL); - ticks = (curTime.tv_sec* 1000) + curTime.tv_usec / 1000; + ticks = (curTime.tv_sec * 1000) + curTime.tv_usec / 1000; return ticks; } @@ -247,97 +255,97 @@ static inline int getKeyState(int key) static void read_keys() { - ws_key_start=0; - ws_key_x4=0; - ws_key_x2=0; - ws_key_x1=0; - ws_key_x3=0; - ws_key_y4=0; - ws_key_y2=0; - ws_key_y1=0; - ws_key_y3=0; - ws_key_button_a=0; - ws_key_button_b=0; + ws_key_start = 0; + ws_key_x4 = 0; + ws_key_x2 = 0; + ws_key_x1 = 0; + ws_key_x3 = 0; + ws_key_y4 = 0; + ws_key_y2 = 0; + ws_key_y1 = 0; + ws_key_y3 = 0; + ws_key_button_a = 0; + ws_key_button_b = 0; if (getKeyState(GLFW_KEY_E)) { - dump_memory(); + dump_memory(); } if (getKeyState(GLFW_KEY_R)) { - printf("Boop\n"); - ws_reset(); + printf("Boop\n"); + ws_reset(); } if (getKeyState(GLFW_KEY_ESCAPE)) { - ws_key_esc = 1; + ws_key_esc = 1; } - if ( getKeyState(GLFW_KEY_UP)) + if (getKeyState(GLFW_KEY_UP)) { - ws_key_x1=1; + ws_key_x1 = 1; } - if ( getKeyState(GLFW_KEY_DOWN)) + if (getKeyState(GLFW_KEY_DOWN)) { - ws_key_x3=1; + ws_key_x3 = 1; } if (getKeyState(GLFW_KEY_RIGHT)) { - ws_key_x2=1; + ws_key_x2 = 1; } if (getKeyState(GLFW_KEY_LEFT)) { - ws_key_x4=1; + ws_key_x4 = 1; } if (getKeyState(GLFW_KEY_ENTER)) { - ws_key_start=1; + ws_key_start = 1; } if (getKeyState(GLFW_KEY_C)) { - ws_key_button_a=1; + ws_key_button_a = 1; } if (getKeyState(GLFW_KEY_X)) { - ws_key_button_b=1; + ws_key_button_b = 1; } if (getKeyState(GLFW_KEY_W)) { - ws_key_y1=1; + ws_key_y1 = 1; } if (getKeyState(GLFW_KEY_A)) { - ws_key_y4=1; + ws_key_y4 = 1; } if (getKeyState(GLFW_KEY_S)) { - ws_key_y3=1; + ws_key_y3 = 1; } if (getKeyState(GLFW_KEY_D)) { - ws_key_y2=1; + ws_key_y2 = 1; } if (getKeyState(GLFW_KEY_O)) { - ws_cyclesByLine+=10; + ws_cyclesByLine += 10; } if (getKeyState(GLFW_KEY_L)) { - ws_cyclesByLine-=10; + ws_cyclesByLine -= 10; } } @@ -365,7 +373,7 @@ void ws_emulate(void) initDisplay(&mainWindow); clearScreen(&mainWindow); updateScreen(&mainWindow); - int16_t *backBuffer = (int16_t *)mainWindow.videoMemory; + int16_t *backBuffer = (int16_t *)mainWindow.videoMemory; dNormalLast = (double)getTicks(); diff --git a/source/emulate.h b/source/emulate.h index 04e691c..a07fca0 100644 --- a/source/emulate.h +++ b/source/emulate.h @@ -1,32 +1,26 @@ +/* + * NewOswan + * emulate.h: + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ +////////////////////////////////////////////////////////////////////////////// +// +// +// +// +// +// +////////////////////////////////////////////////////////////////////////////// #ifndef EMULATE_H #define EMULATE_H #include -#define KEY_ENTER 0x0D -#define KEY_SPACE 0x20 -#define KEY_ESC 0x1b -#define KEY_UP 0x26 -#define KEY_DOWN 0x28 -#define KEY_LEFT 0x25 -#define KEY_RIGHT 0x27 -#define KEY_BUTTON1 0x57 -#define KEY_BUTTON2 0x58 - -#define GUI_COMMAND_NONE 0 -#define GUI_COMMAND_RESET 1 -#define GUI_COMMAND_SCHEME_CHANGE 2 -#define GUI_COMMAND_FILTER_CHANGE 3 - - -extern char app_window_title[256]; -extern int app_gameRunning; -extern int app_terminate; -extern int app_fullscreen; -extern int app_rotated; - +extern char app_window_title[256]; +extern int app_terminate; void ws_emulate(void); - #endif /* EMULATE_H */ \ No newline at end of file diff --git a/source/gpu.c b/source/gpu.c index b740032..4f4f28d 100644 --- a/source/gpu.c +++ b/source/gpu.c @@ -37,14 +37,11 @@ // // //////////////////////////////////////////////////////////////////////////////// -extern uint8_t *internalRam; +extern uint8_t *internalRam; enum VideoModes { - DISPLAY_MODE_GRAY = 0, - DISPLAY_MODE_2BPP = 4, - DISPLAY_MODE_P_4BPP = 7, - DISPLAY_MODE_L_4BPP = 6, + DISPLAY_MODE_GRAY = 0, DISPLAY_MODE_2BPP = 4, DISPLAY_MODE_P_4BPP = 7, DISPLAY_MODE_L_4BPP = 6, }; //////////////////////////////////////////////////////////////////////////////// @@ -58,15 +55,14 @@ enum VideoModes // // //////////////////////////////////////////////////////////////////////////////// -#define RGB555(R,G,B) ((((int)(R))<<10)|(((int)(G))<<5)|((int)(B))) - -uint8_t ws_gpu_operatingInColor; -uint8_t ws_videoMode; -uint8_t ws_gpu_scanline=0; -int16_t ws_palette[16*4]; -int8_t ws_paletteColors[8]; -int16_t wsc_palette[16*16]; +#define RGB555(R, G, B) ((((int)(R))<<10)|(((int)(G))<<5)|((int)(B))) +uint8_t ws_gpu_operatingInColor = 0; +uint8_t ws_videoMode = DISPLAY_MODE_GRAY; +uint8_t ws_gpu_scanline = 0; +int16_t ws_palette[16 * 4]; +int8_t ws_paletteColors[8]; +int16_t wsc_palette[16 * 16]; // white #define SHADE_COLOR_RED 1.00 @@ -92,14 +88,14 @@ int16_t ws_shades[16] = { RGB555(SHADE_COLOR_RED * 0, SHADE_COLOR_GREEN * 0, SHADE_COLOR_BLUE * 0) }; -uint8_t *ws_tile_cache; -uint8_t *ws_hflipped_tile_cache; +uint8_t *ws_tile_cache; +uint8_t *ws_hflipped_tile_cache; -uint8_t *wsc_tile_cache; -uint8_t *wsc_hflipped_tile_cache; +uint8_t *wsc_tile_cache; +uint8_t *wsc_hflipped_tile_cache; -uint8_t *ws_modified_tile; -uint8_t *wsc_modified_tile; +uint8_t *ws_modified_tile; +uint8_t *wsc_modified_tile; //////////////////////////////////////////////////////////////////////////////// // @@ -114,25 +110,25 @@ uint8_t *wsc_modified_tile; //////////////////////////////////////////////////////////////////////////////// void ws_gpu_init(void) { - ws_tile_cache = (uint8_t*)malloc(1024*8*8); - wsc_tile_cache = (uint8_t*)malloc(1024*8*8); + ws_tile_cache = (uint8_t *)malloc(1024 * 8 * 8); + wsc_tile_cache = (uint8_t *)malloc(1024 * 8 * 8); - ws_hflipped_tile_cache = (uint8_t*)malloc(1024*8*8); - wsc_hflipped_tile_cache = (uint8_t*)malloc(1024*8*8); + ws_hflipped_tile_cache = (uint8_t *)malloc(1024 * 8 * 8); + wsc_hflipped_tile_cache = (uint8_t *)malloc(1024 * 8 * 8); - ws_modified_tile = (uint8_t*)malloc(1024); - wsc_modified_tile = (uint8_t*)malloc(1024); + ws_modified_tile = (uint8_t *)malloc(1024); + wsc_modified_tile = (uint8_t *)malloc(1024); - memset(ws_tile_cache,0x00,1024*8*8); - memset(wsc_tile_cache,0x00,1024*8*8); + memset(ws_tile_cache, 0x00, 1024 * 8 * 8); + memset(wsc_tile_cache, 0x00, 1024 * 8 * 8); - memset(ws_hflipped_tile_cache,0x00,1024*8*8); - memset(wsc_hflipped_tile_cache,0x00,1024*8*8); - - memset(ws_modified_tile,0x01,1024); - memset(wsc_modified_tile,0x01,1024); + memset(ws_hflipped_tile_cache, 0x00, 1024 * 8 * 8); + memset(wsc_hflipped_tile_cache, 0x00, 1024 * 8 * 8); + memset(ws_modified_tile, 0x01, 1024); + memset(wsc_modified_tile, 0x01, 1024); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -146,13 +142,14 @@ void ws_gpu_init(void) //////////////////////////////////////////////////////////////////////////////// void ws_gpu_done(void) { - free(ws_tile_cache); - free(wsc_tile_cache); - free(ws_hflipped_tile_cache); - free(wsc_hflipped_tile_cache); - free(ws_modified_tile); - free(wsc_modified_tile); + free(ws_tile_cache); + free(wsc_tile_cache); + free(ws_hflipped_tile_cache); + free(wsc_hflipped_tile_cache); + free(ws_modified_tile); + free(wsc_modified_tile); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -191,10 +188,11 @@ void ws_gpu_changeVideoMode(uint8_t value) //////////////////////////////////////////////////////////////////////////////// void ws_gpu_reset(void) { - memset(ws_modified_tile,0x01,1024); - ws_gpu_scanline=0; - ws_gpu_changeVideoMode(0x00); + memset(ws_modified_tile, 0x01, 1024); + ws_gpu_scanline = 0; + ws_gpu_changeVideoMode(0x00); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -208,8 +206,9 @@ void ws_gpu_reset(void) //////////////////////////////////////////////////////////////////////////////// void ws_gpu_clearCache(void) { - memset(ws_modified_tile,0x01,1024); + memset(ws_modified_tile, 0x01, 1024); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -221,220 +220,218 @@ void ws_gpu_clearCache(void) // // //////////////////////////////////////////////////////////////////////////////// -uint8_t *ws_tileCache_getTileRow(uint32_t tileIndex, uint32_t line, - uint32_t vFlip, uint32_t hFlip, uint32_t bank) +uint8_t *ws_tileCache_getTileRow(uint32_t tileIndex, uint32_t line, uint32_t vFlip, uint32_t hFlip, uint32_t bank) { - if (ws_gpu_operatingInColor) - { - if (bank) - { - tileIndex+=512; - } + if (ws_gpu_operatingInColor) + { + if (bank) + { + tileIndex += 512; + } - // need to update tile cache ? - // 4 colors tiles - if ((ws_videoMode == DISPLAY_MODE_2BPP) && ( ws_modified_tile[tileIndex]) ) - { - uint8_t *tileInCachePtr = &wsc_tile_cache[tileIndex<<6]; - uint8_t *hflippedTileInCachePtr = &wsc_hflipped_tile_cache[tileIndex<<6]; - uint16_t *tileInRamPtr = (uint16_t*)&internalRam[0x2000+(tileIndex<<4)]; - uint16_t tileLine; + // need to update tile cache ? + // 4 colors tiles + if ((ws_videoMode == DISPLAY_MODE_2BPP) && (ws_modified_tile[tileIndex])) + { + uint8_t *tileInCachePtr = &wsc_tile_cache[tileIndex << 6]; + uint8_t *hflippedTileInCachePtr = &wsc_hflipped_tile_cache[tileIndex << 6]; + uint16_t *tileInRamPtr = (uint16_t *)&internalRam[0x2000 + (tileIndex << 4)]; + uint16_t tileLine; - for (int line=0; line<8; line++) - { - tileLine=*tileInRamPtr++; - - tileInCachePtr[0]=((tileLine&0x80)>>7)|((tileLine&0x8000)>>14); - hflippedTileInCachePtr[7]=tileInCachePtr[0]; - tileInCachePtr[1]=((tileLine&0x40)>>6)|((tileLine&0x4000)>>13); - hflippedTileInCachePtr[6]=tileInCachePtr[1]; - tileInCachePtr[2]=((tileLine&0x20)>>5)|((tileLine&0x2000)>>12); - hflippedTileInCachePtr[5]=tileInCachePtr[2]; - tileInCachePtr[3]=((tileLine&0x10)>>4)|((tileLine&0x1000)>>11); - hflippedTileInCachePtr[4]=tileInCachePtr[3]; - tileInCachePtr[4]=((tileLine&0x08)>>3)|((tileLine&0x0800)>>10); - hflippedTileInCachePtr[3]=tileInCachePtr[4]; - tileInCachePtr[5]=((tileLine&0x04)>>2)|((tileLine&0x0400)>>9); - hflippedTileInCachePtr[2]=tileInCachePtr[5]; - tileInCachePtr[6]=((tileLine&0x02)>>1)|((tileLine&0x0200)>>8); - hflippedTileInCachePtr[1]=tileInCachePtr[6]; - tileInCachePtr[7]=((tileLine&0x01)>>0)|((tileLine&0x0100)>>7); - hflippedTileInCachePtr[0]=tileInCachePtr[7]; - - tileInCachePtr+=8; - hflippedTileInCachePtr+=8; - } - - ws_modified_tile[tileIndex]=0; - } - else if (wsc_modified_tile[tileIndex]) - { - // 16 colors by tile layered mode - if (ws_videoMode == DISPLAY_MODE_L_4BPP) - { - uint8_t *tileInCachePtr = &wsc_tile_cache[tileIndex<<6]; - uint8_t *hflippedTileInCachePtr = &wsc_hflipped_tile_cache[tileIndex<<6]; - uint32_t *tileInRamPtr = (uint32_t*)&internalRam[0x4000+(tileIndex<<5)]; - uint32_t tileLine; - - for (int line=0; line<8; line++) + for (int line = 0 ; line < 8 ; line++) { - tileLine=*tileInRamPtr++; + tileLine = *tileInRamPtr++; - tileInCachePtr[0]=((tileLine&0x00000080)>>7)|((tileLine&0x00008000)>>14)| - ((tileLine&0x00800000)>>21)|((tileLine&0x80000000)>>28); - hflippedTileInCachePtr[7]=tileInCachePtr[0]; + tileInCachePtr[0] = ((tileLine & 0x80) >> 7) | ((tileLine & 0x8000) >> 14); + hflippedTileInCachePtr[7] = tileInCachePtr[0]; + tileInCachePtr[1] = ((tileLine & 0x40) >> 6) | ((tileLine & 0x4000) >> 13); + hflippedTileInCachePtr[6] = tileInCachePtr[1]; + tileInCachePtr[2] = ((tileLine & 0x20) >> 5) | ((tileLine & 0x2000) >> 12); + hflippedTileInCachePtr[5] = tileInCachePtr[2]; + tileInCachePtr[3] = ((tileLine & 0x10) >> 4) | ((tileLine & 0x1000) >> 11); + hflippedTileInCachePtr[4] = tileInCachePtr[3]; + tileInCachePtr[4] = ((tileLine & 0x08) >> 3) | ((tileLine & 0x0800) >> 10); + hflippedTileInCachePtr[3] = tileInCachePtr[4]; + tileInCachePtr[5] = ((tileLine & 0x04) >> 2) | ((tileLine & 0x0400) >> 9); + hflippedTileInCachePtr[2] = tileInCachePtr[5]; + tileInCachePtr[6] = ((tileLine & 0x02) >> 1) | ((tileLine & 0x0200) >> 8); + hflippedTileInCachePtr[1] = tileInCachePtr[6]; + tileInCachePtr[7] = ((tileLine & 0x01) >> 0) | ((tileLine & 0x0100) >> 7); + hflippedTileInCachePtr[0] = tileInCachePtr[7]; - tileInCachePtr[1]=((tileLine&0x00000040)>>6)|((tileLine&0x00004000)>>13)| - ((tileLine&0x00400000)>>20)|((tileLine&0x40000000)>>27); - hflippedTileInCachePtr[6]=tileInCachePtr[1]; - - tileInCachePtr[2]=((tileLine&0x00000020)>>5)|((tileLine&0x00002000)>>12)| - ((tileLine&0x00200000)>>19)|((tileLine&0x20000000)>>26); - hflippedTileInCachePtr[5]=tileInCachePtr[2]; - - tileInCachePtr[3]=((tileLine&0x00000010)>>4)|((tileLine&0x00001000)>>11)| - ((tileLine&0x00100000)>>18)|((tileLine&0x10000000)>>25); - hflippedTileInCachePtr[4]=tileInCachePtr[3]; - - tileInCachePtr[4]=((tileLine&0x00000008)>>3)|((tileLine&0x00000800)>>10)| - ((tileLine&0x00080000)>>17)|((tileLine&0x08000000)>>24); - hflippedTileInCachePtr[3]=tileInCachePtr[4]; - - tileInCachePtr[5]=((tileLine&0x00000004)>>2)|((tileLine&0x00000400)>>9)| - ((tileLine&0x00040000)>>16)|((tileLine&0x04000000)>>23); - hflippedTileInCachePtr[2]=tileInCachePtr[5]; - - tileInCachePtr[6]=((tileLine&0x00000002)>>1)|((tileLine&0x00000200)>>8)| - ((tileLine&0x00020000)>>15)|((tileLine&0x02000000)>>22); - hflippedTileInCachePtr[1]=tileInCachePtr[6]; - - tileInCachePtr[7]=((tileLine&0x00000001)>>0)|((tileLine&0x00000100)>>7)| - ((tileLine&0x00010000)>>14)|((tileLine&0x01000000)>>21); - hflippedTileInCachePtr[0]=tileInCachePtr[7]; - - tileInCachePtr+=8; - hflippedTileInCachePtr+=8; + tileInCachePtr += 8; + hflippedTileInCachePtr += 8; } - } - else + ws_modified_tile[tileIndex] = 0; + } + else if (wsc_modified_tile[tileIndex]) + { + // 16 colors by tile layered mode + if (ws_videoMode == DISPLAY_MODE_L_4BPP) + { + uint8_t *tileInCachePtr = &wsc_tile_cache[tileIndex << 6]; + uint8_t *hflippedTileInCachePtr = &wsc_hflipped_tile_cache[tileIndex << 6]; + uint32_t *tileInRamPtr = (uint32_t *)&internalRam[0x4000 + (tileIndex << 5)]; + uint32_t tileLine; - // 16 colors by tile packed mode + for (int line = 0 ; line < 8 ; line++) + { + tileLine = *tileInRamPtr++; + + tileInCachePtr[0] = ((tileLine & 0x00000080) >> 7) | ((tileLine & 0x00008000) >> 14) | + ((tileLine & 0x00800000) >> 21) | ((tileLine & 0x80000000) >> 28); + hflippedTileInCachePtr[7] = tileInCachePtr[0]; + + tileInCachePtr[1] = ((tileLine & 0x00000040) >> 6) | ((tileLine & 0x00004000) >> 13) | + ((tileLine & 0x00400000) >> 20) | ((tileLine & 0x40000000) >> 27); + hflippedTileInCachePtr[6] = tileInCachePtr[1]; + + tileInCachePtr[2] = ((tileLine & 0x00000020) >> 5) | ((tileLine & 0x00002000) >> 12) | + ((tileLine & 0x00200000) >> 19) | ((tileLine & 0x20000000) >> 26); + hflippedTileInCachePtr[5] = tileInCachePtr[2]; + + tileInCachePtr[3] = ((tileLine & 0x00000010) >> 4) | ((tileLine & 0x00001000) >> 11) | + ((tileLine & 0x00100000) >> 18) | ((tileLine & 0x10000000) >> 25); + hflippedTileInCachePtr[4] = tileInCachePtr[3]; + + tileInCachePtr[4] = ((tileLine & 0x00000008) >> 3) | ((tileLine & 0x00000800) >> 10) | + ((tileLine & 0x00080000) >> 17) | ((tileLine & 0x08000000) >> 24); + hflippedTileInCachePtr[3] = tileInCachePtr[4]; + + tileInCachePtr[5] = ((tileLine & 0x00000004) >> 2) | ((tileLine & 0x00000400) >> 9) | + ((tileLine & 0x00040000) >> 16) | ((tileLine & 0x04000000) >> 23); + hflippedTileInCachePtr[2] = tileInCachePtr[5]; + + tileInCachePtr[6] = ((tileLine & 0x00000002) >> 1) | ((tileLine & 0x00000200) >> 8) | + ((tileLine & 0x00020000) >> 15) | ((tileLine & 0x02000000) >> 22); + hflippedTileInCachePtr[1] = tileInCachePtr[6]; + + tileInCachePtr[7] = ((tileLine & 0x00000001) >> 0) | ((tileLine & 0x00000100) >> 7) | + ((tileLine & 0x00010000) >> 14) | ((tileLine & 0x01000000) >> 21); + hflippedTileInCachePtr[0] = tileInCachePtr[7]; + + tileInCachePtr += 8; + hflippedTileInCachePtr += 8; + } + } + else + + // 16 colors by tile packed mode if (ws_videoMode == DISPLAY_MODE_P_4BPP) { - uint8_t *tileInCachePtr = &wsc_tile_cache[tileIndex<<6]; - uint8_t *hflippedTileInCachePtr = &wsc_hflipped_tile_cache[tileIndex<<6]; - uint32_t *tileInRamPtr = (uint32_t*)&internalRam[0x4000+(tileIndex<<5)]; - uint32_t tileLine; + uint8_t *tileInCachePtr = &wsc_tile_cache[tileIndex << 6]; + uint8_t *hflippedTileInCachePtr = &wsc_hflipped_tile_cache[tileIndex << 6]; + uint32_t *tileInRamPtr = (uint32_t *)&internalRam[0x4000 + (tileIndex << 5)]; + uint32_t tileLine; - for (int line=0; line<8; line++) - { - tileLine=*tileInRamPtr++; + for (int line = 0 ; line < 8 ; line++) + { + tileLine = *tileInRamPtr++; - tileInCachePtr[0]=(tileLine>>4)&0x0f; - hflippedTileInCachePtr[7]=tileInCachePtr[0]; - tileInCachePtr[1]=(tileLine>>0)&0x0f; - hflippedTileInCachePtr[6]=tileInCachePtr[1]; - tileInCachePtr[2]=(tileLine>>12)&0x0f; - hflippedTileInCachePtr[5]=tileInCachePtr[2]; - tileInCachePtr[3]=(tileLine>>8)&0x0f; - hflippedTileInCachePtr[4]=tileInCachePtr[3]; - tileInCachePtr[4]=(tileLine>>20)&0x0f; - hflippedTileInCachePtr[3]=tileInCachePtr[4]; - tileInCachePtr[5]=(tileLine>>16)&0x0f; - hflippedTileInCachePtr[2]=tileInCachePtr[5]; - tileInCachePtr[6]=(tileLine>>28)&0x0f; - hflippedTileInCachePtr[1]=tileInCachePtr[6]; - tileInCachePtr[7]=(tileLine>>24)&0x0f; - hflippedTileInCachePtr[0]=tileInCachePtr[7]; + tileInCachePtr[0] = (tileLine >> 4) & 0x0f; + hflippedTileInCachePtr[7] = tileInCachePtr[0]; + tileInCachePtr[1] = (tileLine >> 0) & 0x0f; + hflippedTileInCachePtr[6] = tileInCachePtr[1]; + tileInCachePtr[2] = (tileLine >> 12) & 0x0f; + hflippedTileInCachePtr[5] = tileInCachePtr[2]; + tileInCachePtr[3] = (tileLine >> 8) & 0x0f; + hflippedTileInCachePtr[4] = tileInCachePtr[3]; + tileInCachePtr[4] = (tileLine >> 20) & 0x0f; + hflippedTileInCachePtr[3] = tileInCachePtr[4]; + tileInCachePtr[5] = (tileLine >> 16) & 0x0f; + hflippedTileInCachePtr[2] = tileInCachePtr[5]; + tileInCachePtr[6] = (tileLine >> 28) & 0x0f; + hflippedTileInCachePtr[1] = tileInCachePtr[6]; + tileInCachePtr[7] = (tileLine >> 24) & 0x0f; + hflippedTileInCachePtr[0] = tileInCachePtr[7]; - tileInCachePtr+=8; - hflippedTileInCachePtr+=8; - - } + tileInCachePtr += 8; + hflippedTileInCachePtr += 8; + } } else { - // unknown mode + // unknown mode } - // tile cache updated - wsc_modified_tile[tileIndex]=0; - } + // tile cache updated + wsc_modified_tile[tileIndex] = 0; + } - if (vFlip) - { - line=7-line; - } + if (vFlip) + { + line = 7 - line; + } - if (hFlip) - { - return(&wsc_hflipped_tile_cache[(tileIndex<<6)+(line<<3)]); - } - else - { - return(&wsc_tile_cache[(tileIndex<<6)+(line<<3)]); - } + if (hFlip) + { + return (&wsc_hflipped_tile_cache[(tileIndex << 6) + (line << 3)]); + } + else + { + return (&wsc_tile_cache[(tileIndex << 6) + (line << 3)]); + } - } - else - { - // need to update tile cache ? - if (ws_modified_tile[tileIndex]) - { - uint8_t *tileInCachePtr = &ws_tile_cache[tileIndex<<6]; - uint8_t *hflippedTileInCachePtr = &ws_hflipped_tile_cache[(tileIndex<<6)+7]; - uint32_t *tileInRamPtr = (uint32_t*)&internalRam[0x2000+(tileIndex<<4)]; - uint32_t tileLine; + } + else + { + // need to update tile cache ? + if (ws_modified_tile[tileIndex]) + { + uint8_t *tileInCachePtr = &ws_tile_cache[tileIndex << 6]; + uint8_t *hflippedTileInCachePtr = &ws_hflipped_tile_cache[(tileIndex << 6) + 7]; + uint32_t *tileInRamPtr = (uint32_t *)&internalRam[0x2000 + (tileIndex << 4)]; + uint32_t tileLine; - for (int line=0; line<4; line++) - { - tileLine=*tileInRamPtr++; + for (int line = 0 ; line < 4 ; line++) + { + tileLine = *tileInRamPtr++; - *hflippedTileInCachePtr--=*tileInCachePtr++=((tileLine&0x80)>>7)|((tileLine&0x8000)>>14); - *hflippedTileInCachePtr--=*tileInCachePtr++=((tileLine&0x40)>>6)|((tileLine&0x4000)>>13); - *hflippedTileInCachePtr--=*tileInCachePtr++=((tileLine&0x20)>>5)|((tileLine&0x2000)>>12); - *hflippedTileInCachePtr--=*tileInCachePtr++=((tileLine&0x10)>>4)|((tileLine&0x1000)>>11); - *hflippedTileInCachePtr--=*tileInCachePtr++=((tileLine&0x08)>>3)|((tileLine&0x0800)>>10); - *hflippedTileInCachePtr--=*tileInCachePtr++=((tileLine&0x04)>>2)|((tileLine&0x0400)>>9); - *hflippedTileInCachePtr--=*tileInCachePtr++=((tileLine&0x02)>>1)|((tileLine&0x0200)>>8); - *hflippedTileInCachePtr--=*tileInCachePtr++=((tileLine&0x01)>>0)|((tileLine&0x0100)>>7); - hflippedTileInCachePtr+=16; - tileLine>>=16; - *hflippedTileInCachePtr--=*tileInCachePtr++=((tileLine&0x80)>>7)|((tileLine&0x8000)>>14); - *hflippedTileInCachePtr--=*tileInCachePtr++=((tileLine&0x40)>>6)|((tileLine&0x4000)>>13); - *hflippedTileInCachePtr--=*tileInCachePtr++=((tileLine&0x20)>>5)|((tileLine&0x2000)>>12); - *hflippedTileInCachePtr--=*tileInCachePtr++=((tileLine&0x10)>>4)|((tileLine&0x1000)>>11); - *hflippedTileInCachePtr--=*tileInCachePtr++=((tileLine&0x08)>>3)|((tileLine&0x0800)>>10); - *hflippedTileInCachePtr--=*tileInCachePtr++=((tileLine&0x04)>>2)|((tileLine&0x0400)>>9); - *hflippedTileInCachePtr--=*tileInCachePtr++=((tileLine&0x02)>>1)|((tileLine&0x0200)>>8); - *hflippedTileInCachePtr--=*tileInCachePtr++=((tileLine&0x01)>>0)|((tileLine&0x0100)>>7); - hflippedTileInCachePtr+=16; - } + *hflippedTileInCachePtr-- = *tileInCachePtr++ = ((tileLine & 0x80) >> 7) | ((tileLine & 0x8000) >> 14); + *hflippedTileInCachePtr-- = *tileInCachePtr++ = ((tileLine & 0x40) >> 6) | ((tileLine & 0x4000) >> 13); + *hflippedTileInCachePtr-- = *tileInCachePtr++ = ((tileLine & 0x20) >> 5) | ((tileLine & 0x2000) >> 12); + *hflippedTileInCachePtr-- = *tileInCachePtr++ = ((tileLine & 0x10) >> 4) | ((tileLine & 0x1000) >> 11); + *hflippedTileInCachePtr-- = *tileInCachePtr++ = ((tileLine & 0x08) >> 3) | ((tileLine & 0x0800) >> 10); + *hflippedTileInCachePtr-- = *tileInCachePtr++ = ((tileLine & 0x04) >> 2) | ((tileLine & 0x0400) >> 9); + *hflippedTileInCachePtr-- = *tileInCachePtr++ = ((tileLine & 0x02) >> 1) | ((tileLine & 0x0200) >> 8); + *hflippedTileInCachePtr-- = *tileInCachePtr++ = ((tileLine & 0x01) >> 0) | ((tileLine & 0x0100) >> 7); + hflippedTileInCachePtr += 16; + tileLine >>= 16; + *hflippedTileInCachePtr-- = *tileInCachePtr++ = ((tileLine & 0x80) >> 7) | ((tileLine & 0x8000) >> 14); + *hflippedTileInCachePtr-- = *tileInCachePtr++ = ((tileLine & 0x40) >> 6) | ((tileLine & 0x4000) >> 13); + *hflippedTileInCachePtr-- = *tileInCachePtr++ = ((tileLine & 0x20) >> 5) | ((tileLine & 0x2000) >> 12); + *hflippedTileInCachePtr-- = *tileInCachePtr++ = ((tileLine & 0x10) >> 4) | ((tileLine & 0x1000) >> 11); + *hflippedTileInCachePtr-- = *tileInCachePtr++ = ((tileLine & 0x08) >> 3) | ((tileLine & 0x0800) >> 10); + *hflippedTileInCachePtr-- = *tileInCachePtr++ = ((tileLine & 0x04) >> 2) | ((tileLine & 0x0400) >> 9); + *hflippedTileInCachePtr-- = *tileInCachePtr++ = ((tileLine & 0x02) >> 1) | ((tileLine & 0x0200) >> 8); + *hflippedTileInCachePtr-- = *tileInCachePtr++ = ((tileLine & 0x01) >> 0) | ((tileLine & 0x0100) >> 7); + hflippedTileInCachePtr += 16; + } - // tile cache updated - ws_modified_tile[tileIndex]=0; - } + // tile cache updated + ws_modified_tile[tileIndex] = 0; + } - if (vFlip) - { - line=7-line; - } + if (vFlip) + { + line = 7 - line; + } - if (hFlip) - { - return(&ws_hflipped_tile_cache[(tileIndex<<6)+(line<<3)]); - } - else - { - return(&ws_tile_cache[(tileIndex<<6)+(line<<3)]); - } - } + if (hFlip) + { + return (&ws_hflipped_tile_cache[(tileIndex << 6) + (line << 3)]); + } + else + { + return (&ws_tile_cache[(tileIndex << 6) + (line << 3)]); + } + } - return(NULL); + return (NULL); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -446,90 +443,90 @@ uint8_t *ws_tileCache_getTileRow(uint32_t tileIndex, uint32_t line, // // //////////////////////////////////////////////////////////////////////////////// -void ws_drawClippedSpriteLine(int16_t *framebuffer, uint16_t scanline, - uint32_t x, uint32_t y, uint32_t tileIndex, uint32_t paletteIndex, - uint32_t vFlip, uint32_t hFlip, - uint32_t clip_x0, uint32_t clip_y0, uint32_t clip_x1, uint32_t clip_y1) +void ws_drawClippedSpriteLine(int16_t *framebuffer, uint16_t scanline, uint32_t x, uint32_t y, uint32_t tileIndex, + uint32_t paletteIndex, uint32_t vFlip, uint32_t hFlip, uint32_t clip_x0, uint32_t clip_y0, + uint32_t clip_x1, uint32_t clip_y1) { - if ((scanline(y+7))) - { - return; - } + if ((scanline < y) || (scanline > (y + 7))) + { + return; + } - if (((x+7=clip_x1))) - { - return; - } + if (((x + 7 < clip_x0) || (x >= clip_x1))) + { + return; + } - if(scanline>clip_y0 && scanline clip_y0 && scanline < clip_y1) + { + return; + } - uint8_t *ws_tileRow=ws_tileCache_getTileRow(tileIndex,(scanline-y)&0x07,hFlip,vFlip, 0); - uint16_t nbPixels=8; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileIndex, (scanline - y) & 0x07, hFlip, vFlip, 0); + uint16_t nbPixels = 8; - if (xclip_x1) - { - nbPixels=(clip_x1-x); - } + if (x + nbPixels > clip_x1) + { + nbPixels = (clip_x1 - x); + } - framebuffer+=x; + framebuffer += x; - if (ws_gpu_operatingInColor) - { - int16_t *wsc_paletteAlias=&wsc_palette[paletteIndex<<4]; + if (ws_gpu_operatingInColor) + { + int16_t *wsc_paletteAlias = &wsc_palette[paletteIndex << 4]; - while (nbPixels) - { - if (*ws_tileRow) - { - *framebuffer=wsc_paletteAlias[*ws_tileRow]; - } - - framebuffer++; - ws_tileRow++; - nbPixels--; - } - } - else - { - int16_t *ws_paletteAlias=&ws_palette[paletteIndex<<2]; - - if (paletteIndex&0x04) - { - while (nbPixels) - { + while (nbPixels) + { if (*ws_tileRow) { - *framebuffer=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + *framebuffer = wsc_paletteAlias[*ws_tileRow]; } framebuffer++; ws_tileRow++; nbPixels--; - } - } - else - { - while (nbPixels) - { - *framebuffer=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - framebuffer++; - ws_tileRow++; - nbPixels--; - } - } - } + } + } + else + { + int16_t *ws_paletteAlias = &ws_palette[paletteIndex << 2]; + + if (paletteIndex & 0x04) + { + while (nbPixels) + { + if (*ws_tileRow) + { + *framebuffer = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + framebuffer++; + ws_tileRow++; + nbPixels--; + } + } + else + { + while (nbPixels) + { + *framebuffer = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + framebuffer++; + ws_tileRow++; + nbPixels--; + } + } + } } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -544,1351 +541,1351 @@ void ws_drawClippedSpriteLine(int16_t *framebuffer, uint16_t scanline, void ws_gpu_renderScanline(int16_t *framebuffer) { - if (ws_gpu_scanline>143) - { - return; - } - - framebuffer+=(224*ws_gpu_scanline); - - // fill with background color - int16_t backgroundColor; - - if (ws_gpu_operatingInColor) - { - backgroundColor=wsc_palette[ws_ioRam[0x01]]; - } - else - { - backgroundColor=ws_shades[ws_paletteColors[ws_palette[((ws_ioRam[0x01]&0xf0)>>2)+(ws_ioRam[0x01]&0x03)]]]; - } - - for (int i=0; i<224; i++) - { - framebuffer[i]=backgroundColor; - } - - - // render background layer - if (ws_ioRam[0x00]&0x01) - { - int ws_bgScroll_x=ws_ioRam[0x10]; - int ws_bgScroll_y=ws_ioRam[0x11]; - - // seek to the first tile - ws_bgScroll_y=(ws_bgScroll_y+ws_gpu_scanline)&0xff; - - // note: byte ordering assumptions! - int ws_currentTile=(ws_bgScroll_x>>3); - uint16_t *ws_bgScrollRamBase=(uint16_t*)(internalRam+(((uint32_t)ws_ioRam[0x07]&0x0f)<<11)+ - ((ws_bgScroll_y&0xfff8)<<3)); - - int lineInTile = ws_bgScroll_y&0x07; - int columnInTile = ws_bgScroll_x&0x07; - - int16_t *scanlinePtr=framebuffer; - - if (ws_gpu_operatingInColor) - { - // render the first clipped tile - if (columnInTile) - { - uint16_t tileInfo=ws_bgScrollRamBase[ws_currentTile&0x1f]; - ws_currentTile++; - uint8_t *ws_tileRow=ws_tileCache_getTileRow( tileInfo&0x1ff, lineInTile, tileInfo&0x8000, tileInfo&0x4000, tileInfo&0x2000); - int16_t *wsc_paletteAlias=&wsc_palette[((tileInfo>>9)&0x0f)<<4]; - ws_tileRow+=columnInTile; - - for (int i=columnInTile; i<8; i++) - { - if (*ws_tileRow) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } - - scanlinePtr++; - ws_tileRow++; - } - } - - // render the tiles between them - int nbTiles=28; - - if (columnInTile) - { - nbTiles=27; - } - - for (int i=0; i>9)&0x0f)<<4]; - - if (*ws_tileRow) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } - - scanlinePtr++; - ws_tileRow++; - - if (*ws_tileRow) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } - - scanlinePtr++; - ws_tileRow++; - - if (*ws_tileRow) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } - - scanlinePtr++; - ws_tileRow++; - - if (*ws_tileRow) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } - - scanlinePtr++; - ws_tileRow++; - - if (*ws_tileRow) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } - - scanlinePtr++; - ws_tileRow++; - - if (*ws_tileRow) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } - - scanlinePtr++; - ws_tileRow++; - - if (*ws_tileRow) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } - - scanlinePtr++; - ws_tileRow++; - - if (*ws_tileRow) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } - - scanlinePtr++; - ws_tileRow++; - } - - // render the last clipped tile - if (columnInTile) - { - uint16_t tileInfo=ws_bgScrollRamBase[ws_currentTile&0x1f]; - ws_currentTile++; - uint8_t *ws_tileRow=ws_tileCache_getTileRow( tileInfo&0x1ff, lineInTile, - tileInfo&0x8000, tileInfo&0x4000, tileInfo&0x2000); - int16_t *wsc_paletteAlias=&wsc_palette[((tileInfo>>9)&0x0f)<<4]; - - - for (int i=0; i>9)&0x0f)<<2]; - ws_tileRow+=columnInTile; - - if ((tileInfo>>9)&0x04) - { - for (int i=columnInTile; i<8; i++) - { - if (*ws_tileRow) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - scanlinePtr++; - ws_tileRow++; - } - } - else - { - for (int i=columnInTile; i<8; i++) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - scanlinePtr++; - ws_tileRow++; - } - } - } - - int nbTiles=28; - - if (columnInTile) - { - nbTiles=27; - } - - for (int i=0; i>9)&0x0f)<<2]; - - if ((tileInfo>>9)&0x04) - { - if (*ws_tileRow) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - scanlinePtr++; - ws_tileRow++; - - if (*ws_tileRow) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - scanlinePtr++; - ws_tileRow++; - - if (*ws_tileRow) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - scanlinePtr++; - ws_tileRow++; - - if (*ws_tileRow) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - scanlinePtr++; - ws_tileRow++; - - if (*ws_tileRow) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - scanlinePtr++; - ws_tileRow++; - - if (*ws_tileRow) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - scanlinePtr++; - ws_tileRow++; - - if (*ws_tileRow) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - scanlinePtr++; - ws_tileRow++; - - if (*ws_tileRow) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - scanlinePtr++; - ws_tileRow++; - } - else - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - scanlinePtr++; - ws_tileRow++; - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - scanlinePtr++; - ws_tileRow++; - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - scanlinePtr++; - ws_tileRow++; - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - scanlinePtr++; - ws_tileRow++; - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - scanlinePtr++; - ws_tileRow++; - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - scanlinePtr++; - ws_tileRow++; - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - scanlinePtr++; - ws_tileRow++; - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - scanlinePtr++; - ws_tileRow++; - } - - } - - // render the last clipped tile - if (columnInTile) - { - uint16_t tileInfo=ws_bgScrollRamBase[ws_currentTile&0x1f]; - ws_currentTile++; - uint8_t *ws_tileRow=ws_tileCache_getTileRow( tileInfo&0x1ff, lineInTile, - tileInfo&0x8000, tileInfo&0x4000, tileInfo&0x2000); - int16_t *ws_paletteAlias=&ws_palette[((tileInfo>>9)&0x0f)<<2]; - - - if ((tileInfo>>9)&0x04) - { - for (int i=0; iws_ioRam[0x05]; i--) - { - uint32_t spr=*ws_sprRamBase--; - - if (!(spr&0x2000)) - { - // sprite window on ? - if ((ws_ioRam[0x00]&0x08)&&(spr&0x1000)&&(ws_sprWindow_x0!=ws_sprWindow_x1)) - { - ws_drawClippedSpriteLine(framebuffer,ws_gpu_scanline,(spr&0xff000000)>>24, (spr&0x00ff0000)>>16, - spr&0x1ff,8+((spr&0xe00)>>9),spr&0x4000,spr&0x8000, - ws_sprWindow_x0,ws_sprWindow_y0,ws_sprWindow_x1,ws_sprWindow_y1); - } - else - { - ws_drawClippedSpriteLine(framebuffer,ws_gpu_scanline,(spr&0xff000000)>>24, (spr&0x00ff0000)>>16, - spr&0x1ff,8+((spr&0xe00)>>9),spr&0x4000,spr&0x8000, - //0,0,224,144); - 0,0,224,0); - } - } - } - } - - // render foreground layer - if (ws_ioRam[0x00]&0x02) - { - int ws_fgWindow_x0=ws_ioRam[0x08]; - int ws_fgWindow_x1=ws_ioRam[0x0a]; - int ws_fgScroll_x=ws_ioRam[0x12]; - int ws_fgScroll_y=ws_ioRam[0x13]; - - int windowMode = ws_ioRam[0x00]&0x30; - - // seek to the first tile - ws_fgScroll_y=(ws_fgScroll_y+ws_gpu_scanline)&0xff; - - // note: byte ordering assumptions! - int ws_currentTile=(ws_fgScroll_x>>3); - uint16_t *ws_fgScrollRamBase=(uint16_t*)(internalRam+(((uint32_t)ws_ioRam[0x07]&0xf0)<<7)+ - ((ws_fgScroll_y&0xfff8)<<3)); - - int lineInTile = ws_fgScroll_y&0x07; - int columnInTile = ws_fgScroll_x&0x07; - - int16_t *scanlinePtr=framebuffer; - - - // window disabled - if (!windowMode) - { - if (ws_gpu_operatingInColor) - { + if (ws_gpu_scanline > 143) + { + return; + } + framebuffer += (224 * ws_gpu_scanline); + + // fill with background color + int16_t backgroundColor; + + if (ws_gpu_operatingInColor) + { + backgroundColor = wsc_palette[ws_ioRam[0x01]]; + } + else + { + backgroundColor = ws_shades[ws_paletteColors[ws_palette[((ws_ioRam[0x01] & 0xf0) >> 2) + + (ws_ioRam[0x01] & 0x03)]]]; + } + + for (int i = 0 ; i < 224 ; i++) + { + framebuffer[i] = backgroundColor; + } + + // render background layer + if (ws_ioRam[0x00] & 0x01) + { + int ws_bgScroll_x = ws_ioRam[0x10]; + int ws_bgScroll_y = ws_ioRam[0x11]; + + // seek to the first tile + ws_bgScroll_y = (ws_bgScroll_y + ws_gpu_scanline) & 0xff; + + // note: byte ordering assumptions! + int ws_currentTile = (ws_bgScroll_x >> 3); + uint16_t *ws_bgScrollRamBase = (uint16_t *)(internalRam + (((uint32_t)ws_ioRam[0x07] & 0x0f) << 11) + + ((ws_bgScroll_y & 0xfff8) << 3)); + + int lineInTile = ws_bgScroll_y & 0x07; + int columnInTile = ws_bgScroll_x & 0x07; + + int16_t *scanlinePtr = framebuffer; + + if (ws_gpu_operatingInColor) + { // render the first clipped tile if (columnInTile) { - uint16_t tileInfo=ws_fgScrollRamBase[ws_currentTile&0x1f]; - ws_currentTile++; - uint8_t *ws_tileRow=ws_tileCache_getTileRow( - tileInfo&0x1ff, lineInTile, - tileInfo&0x8000, - tileInfo&0x4000, - tileInfo&0x2000); + uint16_t tileInfo = ws_bgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *wsc_paletteAlias = &wsc_palette[((tileInfo >> 9) & 0x0f) << 4]; + ws_tileRow += columnInTile; - int16_t *wsc_paletteAlias=&wsc_palette[((tileInfo>>9)&0x0f)<<4]; + for (int i = columnInTile ; i < 8 ; i++) + { + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - ws_tileRow+=columnInTile; - - for (int i=columnInTile; i<8; i++) - { - if (*ws_tileRow) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } - - scanlinePtr++; - ws_tileRow++; - } + scanlinePtr++; + ws_tileRow++; + } } // render the tiles between them - int nbTiles=28; + int nbTiles = 28; if (columnInTile) { - nbTiles=27; + nbTiles = 27; } - for (int i=0; i>9)&0x0f)<<4]; + uint16_t tileInfo = ws_bgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *wsc_paletteAlias = &wsc_palette[((tileInfo >> 9) & 0x0f) << 4]; + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if (*ws_tileRow) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; - scanlinePtr++; - ws_tileRow++; + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if (*ws_tileRow) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; - scanlinePtr++; - ws_tileRow++; + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if (*ws_tileRow) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; - scanlinePtr++; - ws_tileRow++; + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if (*ws_tileRow) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; - scanlinePtr++; - ws_tileRow++; + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if (*ws_tileRow) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; - scanlinePtr++; - ws_tileRow++; + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if (*ws_tileRow) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; - scanlinePtr++; - ws_tileRow++; + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if (*ws_tileRow) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; - scanlinePtr++; - ws_tileRow++; + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if (*ws_tileRow) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } - - scanlinePtr++; - ws_tileRow++; + scanlinePtr++; + ws_tileRow++; } // render the last clipped tile if (columnInTile) { - uint16_t tileInfo=ws_fgScrollRamBase[ws_currentTile&0x1f]; - ws_currentTile++; - uint8_t *ws_tileRow=ws_tileCache_getTileRow( tileInfo&0x1ff, lineInTile, - tileInfo&0x8000, tileInfo&0x4000, tileInfo&0x2000); - int16_t *wsc_paletteAlias=&wsc_palette[((tileInfo>>9)&0x0f)<<4]; + uint16_t tileInfo = ws_bgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *wsc_paletteAlias = &wsc_palette[((tileInfo >> 9) & 0x0f) << 4]; - for (int i=0; i>9)&0x0f)<<2]; - ws_tileRow+=columnInTile; + int16_t *ws_paletteAlias = &ws_palette[((tileInfo >> 9) & 0x0f) << 2]; + ws_tileRow += columnInTile; - if ((tileInfo>>9)&0x04) - { - for (int i=columnInTile; i<8; i++) - { - if (*ws_tileRow) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + if ((tileInfo >> 9) & 0x04) + { + for (int i = columnInTile ; i < 8 ; i++) + { + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - scanlinePtr++; - ws_tileRow++; - } - } - else - { - for (int i=columnInTile; i<8; i++) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - scanlinePtr++; - ws_tileRow++; - } - } + scanlinePtr++; + ws_tileRow++; + } + } + else + { + for (int i = columnInTile ; i < 8 ; i++) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + } + } } - int nbTiles=28; + int nbTiles = 28; if (columnInTile) { - nbTiles=27; + nbTiles = 27; } - for (int i=0; i>9)&0x0f)<<2]; + uint16_t tileInfo = ws_bgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *ws_paletteAlias = &ws_palette[((tileInfo >> 9) & 0x0f) << 2]; - if ((tileInfo>>9)&0x04) - { - if (*ws_tileRow) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + if ((tileInfo >> 9) & 0x04) + { + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - scanlinePtr++; - ws_tileRow++; + scanlinePtr++; + ws_tileRow++; - if (*ws_tileRow) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - scanlinePtr++; - ws_tileRow++; + scanlinePtr++; + ws_tileRow++; - if (*ws_tileRow) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - scanlinePtr++; - ws_tileRow++; + scanlinePtr++; + ws_tileRow++; - if (*ws_tileRow) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - scanlinePtr++; - ws_tileRow++; + scanlinePtr++; + ws_tileRow++; - if (*ws_tileRow) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - scanlinePtr++; - ws_tileRow++; + scanlinePtr++; + ws_tileRow++; - if (*ws_tileRow) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - scanlinePtr++; - ws_tileRow++; + scanlinePtr++; + ws_tileRow++; - if (*ws_tileRow) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - scanlinePtr++; - ws_tileRow++; + scanlinePtr++; + ws_tileRow++; - if (*ws_tileRow) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + } + else + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + } - scanlinePtr++; - ws_tileRow++; - } - else - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - scanlinePtr++; - ws_tileRow++; - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - scanlinePtr++; - ws_tileRow++; - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - scanlinePtr++; - ws_tileRow++; - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - scanlinePtr++; - ws_tileRow++; - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - scanlinePtr++; - ws_tileRow++; - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - scanlinePtr++; - ws_tileRow++; - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - scanlinePtr++; - ws_tileRow++; - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - scanlinePtr++; - ws_tileRow++; - } } // render the last clipped tile if (columnInTile) { - uint16_t tileInfo=ws_fgScrollRamBase[ws_currentTile&0x1f]; - ws_currentTile++; - uint8_t *ws_tileRow=ws_tileCache_getTileRow( tileInfo&0x1ff, lineInTile, - tileInfo&0x8000, tileInfo&0x4000, tileInfo&0x2000); - int16_t *ws_paletteAlias=&ws_palette[((tileInfo>>9)&0x0f)<<2]; + uint16_t tileInfo = ws_bgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *ws_paletteAlias = &ws_palette[((tileInfo >> 9) & 0x0f) << 2]; - if ((tileInfo>>9)&0x04) - { - for (int i=0; i> 9) & 0x04) + { + for (int i = 0 ; i < columnInTile ; i++) + { + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - scanlinePtr++; - ws_tileRow++; - } - } - else - { - for (int i=0; i ws_ioRam[0x05] ; i--) + { + uint32_t spr = *ws_sprRamBase--; + + if (!(spr & 0x2000)) + { + // sprite window on ? + if ((ws_ioRam[0x00] & 0x08) && (spr & 0x1000) && (ws_sprWindow_x0 != ws_sprWindow_x1)) + { + ws_drawClippedSpriteLine(framebuffer, ws_gpu_scanline, (spr & 0xff000000) >> 24, + (spr & 0x00ff0000) >> 16, spr & 0x1ff, 8 + ((spr & 0xe00) >> 9), + spr & 0x4000, spr & 0x8000, ws_sprWindow_x0, ws_sprWindow_y0, + ws_sprWindow_x1, ws_sprWindow_y1); + } + else + { + ws_drawClippedSpriteLine(framebuffer, ws_gpu_scanline, (spr & 0xff000000) >> 24, + (spr & 0x00ff0000) >> 16, spr & 0x1ff, 8 + ((spr & 0xe00) >> 9), + spr & 0x4000, spr & 0x8000, + //0,0,224,144); + 0, 0, 224, 0); + } + } + } + } + + // render foreground layer + if (ws_ioRam[0x00] & 0x02) + { + int ws_fgWindow_x0 = ws_ioRam[0x08]; + int ws_fgWindow_x1 = ws_ioRam[0x0a]; + int ws_fgScroll_x = ws_ioRam[0x12]; + int ws_fgScroll_y = ws_ioRam[0x13]; + + int windowMode = ws_ioRam[0x00] & 0x30; + + // seek to the first tile + ws_fgScroll_y = (ws_fgScroll_y + ws_gpu_scanline) & 0xff; + + // note: byte ordering assumptions! + int ws_currentTile = (ws_fgScroll_x >> 3); + uint16_t *ws_fgScrollRamBase = (uint16_t *)(internalRam + (((uint32_t)ws_ioRam[0x07] & 0xf0) << 7) + + ((ws_fgScroll_y & 0xfff8) << 3)); + + int lineInTile = ws_fgScroll_y & 0x07; + int columnInTile = ws_fgScroll_x & 0x07; + + int16_t *scanlinePtr = framebuffer; + + + // window disabled + if (!windowMode) + { + if (ws_gpu_operatingInColor) + { + // render the first clipped tile + if (columnInTile) + { + uint16_t tileInfo = ws_fgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + + int16_t *wsc_paletteAlias = &wsc_palette[((tileInfo >> 9) & 0x0f) << 4]; + + ws_tileRow += columnInTile; + + for (int i = columnInTile ; i < 8 ; i++) + { + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } + + scanlinePtr++; + ws_tileRow++; + } + } + + // render the tiles between them + int nbTiles = 28; + + if (columnInTile) + { + nbTiles = 27; + } + + for (int i = 0 ; i < nbTiles ; i++) + { + uint16_t tileInfo = ws_fgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *wsc_paletteAlias = &wsc_palette[((tileInfo >> 9) & 0x0f) << 4]; + + + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } + + scanlinePtr++; + ws_tileRow++; + + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } + + scanlinePtr++; + ws_tileRow++; + + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } + + scanlinePtr++; + ws_tileRow++; + + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } + + scanlinePtr++; + ws_tileRow++; + + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } + + scanlinePtr++; + ws_tileRow++; + + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } + + scanlinePtr++; + ws_tileRow++; + + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } + + scanlinePtr++; + ws_tileRow++; + + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } + + scanlinePtr++; + ws_tileRow++; + } + + // render the last clipped tile + if (columnInTile) + { + uint16_t tileInfo = ws_fgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *wsc_paletteAlias = &wsc_palette[((tileInfo >> 9) & 0x0f) << 4]; + + + for (int i = 0 ; i < columnInTile ; i++) + { + if (*ws_tileRow) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } + + scanlinePtr++; + ws_tileRow++; + } + } + } + else + { + // render the first clipped tile + if (columnInTile) + { + uint16_t tileInfo = ws_fgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + + int16_t *ws_paletteAlias = &ws_palette[((tileInfo >> 9) & 0x0f) << 2]; + ws_tileRow += columnInTile; + + if ((tileInfo >> 9) & 0x04) + { + for (int i = columnInTile ; i < 8 ; i++) + { + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + } + } + else + { + for (int i = columnInTile ; i < 8 ; i++) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + } + } + } + + int nbTiles = 28; + + if (columnInTile) + { + nbTiles = 27; + } + + for (int i = 0 ; i < nbTiles ; i++) + { + uint16_t tileInfo = ws_fgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *ws_paletteAlias = &ws_palette[((tileInfo >> 9) & 0x0f) << 2]; + + if ((tileInfo >> 9) & 0x04) + { + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + } + else + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + } + } + + // render the last clipped tile + if (columnInTile) + { + uint16_t tileInfo = ws_fgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *ws_paletteAlias = &ws_palette[((tileInfo >> 9) & 0x0f) << 2]; + + + if ((tileInfo >> 9) & 0x04) + { + for (int i = 0 ; i < columnInTile ; i++) + { + if (*ws_tileRow) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + } + } + else + { + for (int i = 0 ; i < columnInTile ; i++) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + scanlinePtr++; + ws_tileRow++; + } + } + } + } + } + else + + // foreground layer displayed only inside the window + if (windowMode == 0x20) + { + int column = 0; if (ws_gpu_operatingInColor) { - // render the first clipped tile - if (columnInTile) - { - uint16_t tileInfo=ws_fgScrollRamBase[ws_currentTile&0x1f]; - ws_currentTile++; - uint8_t *ws_tileRow=ws_tileCache_getTileRow( tileInfo&0x1ff, lineInTile, - tileInfo&0x8000, tileInfo&0x4000, tileInfo&0x2000); - int16_t *wsc_paletteAlias=&wsc_palette[((tileInfo>>9)&0x0f)<<4]; + // render the first clipped tile + if (columnInTile) + { + uint16_t tileInfo = ws_fgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *wsc_paletteAlias = &wsc_palette[((tileInfo >> 9) & 0x0f) << 4]; - ws_tileRow+=columnInTile; + ws_tileRow += columnInTile; - for (int i=columnInTile; i<8; i++) - { - if ((*ws_tileRow)&&(column>=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + for (int i = columnInTile ; i < 8 ; i++) + { + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - column++; - scanlinePtr++; - ws_tileRow++; - } - } + column++; + scanlinePtr++; + ws_tileRow++; + } + } - // render the tiles between them - int nbTiles=28; + // render the tiles between them + int nbTiles = 28; - if (columnInTile) - { - nbTiles=27; - } + if (columnInTile) + { + nbTiles = 27; + } - for (int i=0; i>9)&0x0f)<<4]; + for (int i = 0 ; i < nbTiles ; i++) + { + uint16_t tileInfo = ws_fgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *wsc_paletteAlias = &wsc_palette[((tileInfo >> 9) & 0x0f) << 4]; + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if ((*ws_tileRow)&&(column>=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; + column++; - scanlinePtr++; - ws_tileRow++; - column++; + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if ((*ws_tileRow)&&(column>=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; + column++; - scanlinePtr++; - ws_tileRow++; - column++; + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if ((*ws_tileRow)&&(column>=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; + column++; - scanlinePtr++; - ws_tileRow++; - column++; + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if ((*ws_tileRow)&&(column>=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; + column++; - scanlinePtr++; - ws_tileRow++; - column++; + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if ((*ws_tileRow)&&(column>=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; + column++; - scanlinePtr++; - ws_tileRow++; - column++; + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if ((*ws_tileRow)&&(column>=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; + column++; - scanlinePtr++; - ws_tileRow++; - column++; + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if ((*ws_tileRow)&&(column>=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; + column++; - scanlinePtr++; - ws_tileRow++; - column++; + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if ((*ws_tileRow)&&(column>=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; + column++; + } - scanlinePtr++; - ws_tileRow++; - column++; - } - - // render the last clipped tile - if (columnInTile) - { - uint16_t tileInfo=ws_fgScrollRamBase[ws_currentTile&0x1f]; - ws_currentTile++; - uint8_t *ws_tileRow=ws_tileCache_getTileRow( tileInfo&0x1ff, lineInTile, - tileInfo&0x8000, tileInfo&0x4000, tileInfo&0x2000); - int16_t *wsc_paletteAlias=&wsc_palette[((tileInfo>>9)&0x0f)<<4]; + // render the last clipped tile + if (columnInTile) + { + uint16_t tileInfo = ws_fgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *wsc_paletteAlias = &wsc_palette[((tileInfo >> 9) & 0x0f) << 4]; - for (int i=0; i=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + for (int i = 0 ; i < columnInTile ; i++) + { + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - column++; - scanlinePtr++; - ws_tileRow++; - } - } + column++; + scanlinePtr++; + ws_tileRow++; + } + } } else { - // render the first clipped tile - if (columnInTile) - { - uint16_t tileInfo=ws_fgScrollRamBase[ws_currentTile&0x1f]; - ws_currentTile++; - uint8_t *ws_tileRow=ws_tileCache_getTileRow( tileInfo&0x1ff, lineInTile, - tileInfo&0x8000, tileInfo&0x4000, tileInfo&0x2000); + // render the first clipped tile + if (columnInTile) + { + uint16_t tileInfo = ws_fgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); - int16_t *ws_paletteAlias=&ws_palette[((tileInfo>>9)&0x0f)<<2]; - ws_tileRow+=columnInTile; + int16_t *ws_paletteAlias = &ws_palette[((tileInfo >> 9) & 0x0f) << 2]; + ws_tileRow += columnInTile; - for (int i=columnInTile; i<8; i++) - { - if ((*ws_tileRow)&&(column>=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + for (int i = columnInTile ; i < 8 ; i++) + { + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - column++; - scanlinePtr++; - ws_tileRow++; - } - } + column++; + scanlinePtr++; + ws_tileRow++; + } + } - int nbTiles=28; + int nbTiles = 28; - if (columnInTile) - { - nbTiles=27; - } + if (columnInTile) + { + nbTiles = 27; + } - for (int i=0; i>9)&0x0f)<<2]; + for (int i = 0 ; i < nbTiles ; i++) + { + uint16_t tileInfo = ws_fgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *ws_paletteAlias = &ws_palette[((tileInfo >> 9) & 0x0f) << 2]; - if ((*ws_tileRow)&&(column>=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - scanlinePtr++; - ws_tileRow++; - column++; + scanlinePtr++; + ws_tileRow++; + column++; - if ((*ws_tileRow)&&(column>=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - scanlinePtr++; - ws_tileRow++; - column++; + scanlinePtr++; + ws_tileRow++; + column++; - if ((*ws_tileRow)&&(column>=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - scanlinePtr++; - ws_tileRow++; - column++; + scanlinePtr++; + ws_tileRow++; + column++; - if ((*ws_tileRow)&&(column>=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - scanlinePtr++; - ws_tileRow++; - column++; + scanlinePtr++; + ws_tileRow++; + column++; - if ((*ws_tileRow)&&(column>=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - scanlinePtr++; - ws_tileRow++; - column++; + scanlinePtr++; + ws_tileRow++; + column++; - if ((*ws_tileRow)&&(column>=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - scanlinePtr++; - ws_tileRow++; - column++; + scanlinePtr++; + ws_tileRow++; + column++; - if ((*ws_tileRow)&&(column>=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - scanlinePtr++; - ws_tileRow++; - column++; + scanlinePtr++; + ws_tileRow++; + column++; - if ((*ws_tileRow)&&(column>=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - scanlinePtr++; - ws_tileRow++; - column++; - } + scanlinePtr++; + ws_tileRow++; + column++; + } - // render the last clipped tile - if (columnInTile) - { - uint16_t tileInfo=ws_fgScrollRamBase[ws_currentTile&0x1f]; - ws_currentTile++; - uint8_t *ws_tileRow=ws_tileCache_getTileRow( tileInfo&0x1ff, lineInTile, - tileInfo&0x8000, tileInfo&0x4000, tileInfo&0x2000); - int16_t *ws_paletteAlias=&ws_palette[((tileInfo>>9)&0x0f)<<2]; + // render the last clipped tile + if (columnInTile) + { + uint16_t tileInfo = ws_fgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *ws_paletteAlias = &ws_palette[((tileInfo >> 9) & 0x0f) << 2]; - for (int i=0; i=ws_fgWindow_x0)&&(column<=ws_fgWindow_x1)) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } + for (int i = 0 ; i < columnInTile ; i++) + { + if ((*ws_tileRow) && (column >= ws_fgWindow_x0) && (column <= ws_fgWindow_x1)) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } - column++; - scanlinePtr++; - ws_tileRow++; - } - } + column++; + scanlinePtr++; + ws_tileRow++; + } + } } - } - else + } + else // foreground layer displayed only outside the window - if (windowMode==0x30) + if (windowMode == 0x30) + { + int column = 0; + + if (ws_gpu_operatingInColor) { - int column=0; + // render the first clipped tile + if (columnInTile) + { + uint16_t tileInfo = ws_fgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *wsc_paletteAlias = &wsc_palette[((tileInfo >> 9) & 0x0f) << 4]; - if (ws_gpu_operatingInColor) - { - // render the first clipped tile - if (columnInTile) - { - uint16_t tileInfo=ws_fgScrollRamBase[ws_currentTile&0x1f]; - ws_currentTile++; - uint8_t *ws_tileRow=ws_tileCache_getTileRow( tileInfo&0x1ff, lineInTile, - tileInfo&0x8000, tileInfo&0x4000, tileInfo&0x2000); - int16_t *wsc_paletteAlias=&wsc_palette[((tileInfo>>9)&0x0f)<<4]; + ws_tileRow += columnInTile; - ws_tileRow+=columnInTile; - - for (int i=columnInTile; i<8; i++) - { - if ((*ws_tileRow)&&((columnws_fgWindow_x1))) + for (int i = columnInTile ; i < 8 ; i++) + { + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; } column++; scanlinePtr++; ws_tileRow++; - } - } + } + } - // render the tiles between them - int nbTiles=28; + // render the tiles between them + int nbTiles = 28; - if (columnInTile) - { - nbTiles=27; - } + if (columnInTile) + { + nbTiles = 27; + } - for (int i=0; i>9)&0x0f)<<4]; + for (int i = 0 ; i < nbTiles ; i++) + { + uint16_t tileInfo = ws_fgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *wsc_paletteAlias = &wsc_palette[((tileInfo >> 9) & 0x0f) << 4]; + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if ((*ws_tileRow)&&((columnws_fgWindow_x1))) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; + column++; - scanlinePtr++; - ws_tileRow++; - column++; + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if ((*ws_tileRow)&&((columnws_fgWindow_x1))) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; + column++; - scanlinePtr++; - ws_tileRow++; - column++; + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if ((*ws_tileRow)&&((columnws_fgWindow_x1))) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; + column++; - scanlinePtr++; - ws_tileRow++; - column++; + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if ((*ws_tileRow)&&((columnws_fgWindow_x1))) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; + column++; - scanlinePtr++; - ws_tileRow++; - column++; + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if ((*ws_tileRow)&&((columnws_fgWindow_x1))) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; + column++; - scanlinePtr++; - ws_tileRow++; - column++; + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if ((*ws_tileRow)&&((columnws_fgWindow_x1))) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; + column++; - scanlinePtr++; - ws_tileRow++; - column++; + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if ((*ws_tileRow)&&((columnws_fgWindow_x1))) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; + column++; - scanlinePtr++; - ws_tileRow++; - column++; + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) + { + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; + } - if ((*ws_tileRow)&&((columnws_fgWindow_x1))) - { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; - } + scanlinePtr++; + ws_tileRow++; + column++; + } - scanlinePtr++; - ws_tileRow++; - column++; - } - - // render the last clipped tile - if (columnInTile) - { - uint16_t tileInfo=ws_fgScrollRamBase[ws_currentTile&0x1f]; - ws_currentTile++; - uint8_t *ws_tileRow=ws_tileCache_getTileRow( tileInfo&0x1ff, lineInTile, - tileInfo&0x8000, tileInfo&0x4000, tileInfo&0x2000); - int16_t *wsc_paletteAlias=&wsc_palette[((tileInfo>>9)&0x0f)<<4]; + // render the last clipped tile + if (columnInTile) + { + uint16_t tileInfo = ws_fgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *wsc_paletteAlias = &wsc_palette[((tileInfo >> 9) & 0x0f) << 4]; - for (int i=0; iws_fgWindow_x1))) + for (int i = 0 ; i < columnInTile ; i++) + { + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) { - *scanlinePtr=wsc_paletteAlias[*ws_tileRow]; + *scanlinePtr = wsc_paletteAlias[*ws_tileRow]; } column++; scanlinePtr++; ws_tileRow++; - } - } - } - else - { - // render the first clipped tile - if (columnInTile) - { - uint16_t tileInfo=ws_fgScrollRamBase[ws_currentTile&0x1f]; - ws_currentTile++; - uint8_t *ws_tileRow=ws_tileCache_getTileRow( tileInfo&0x1ff, lineInTile, - tileInfo&0x8000, tileInfo&0x4000, tileInfo&0x2000); - - int16_t *ws_paletteAlias=&ws_palette[((tileInfo>>9)&0x0f)<<2]; - ws_tileRow+=columnInTile; - - for (int i=columnInTile; i<8; i++) - { - if ((*ws_tileRow)&&((columnws_fgWindow_x1))) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - column++; - scanlinePtr++; - ws_tileRow++; - } - } - - int nbTiles=28; - - if (columnInTile) - { - nbTiles=27; - } - - for (int i=0; i>9)&0x0f)<<2]; - - if ((*ws_tileRow)&&((columnws_fgWindow_x1))) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - scanlinePtr++; - ws_tileRow++; - column++; - - if ((*ws_tileRow)&&((columnws_fgWindow_x1))) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - scanlinePtr++; - ws_tileRow++; - column++; - - if ((*ws_tileRow)&&((columnws_fgWindow_x1))) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - scanlinePtr++; - ws_tileRow++; - column++; - - if ((*ws_tileRow)&&((columnws_fgWindow_x1))) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - scanlinePtr++; - ws_tileRow++; - column++; - - if ((*ws_tileRow)&&((columnws_fgWindow_x1))) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - scanlinePtr++; - ws_tileRow++; - column++; - - if ((*ws_tileRow)&&((columnws_fgWindow_x1))) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - scanlinePtr++; - ws_tileRow++; - column++; - - if ((*ws_tileRow)&&((columnws_fgWindow_x1))) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - scanlinePtr++; - ws_tileRow++; - column++; - - if ((*ws_tileRow)&&((columnws_fgWindow_x1))) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - scanlinePtr++; - ws_tileRow++; - column++; - } - - // render the last clipped tile - if (columnInTile) - { - uint16_t tileInfo=ws_fgScrollRamBase[ws_currentTile&0x1f]; - ws_currentTile++; - uint8_t *ws_tileRow=ws_tileCache_getTileRow( tileInfo&0x1ff, lineInTile, - tileInfo&0x8000, tileInfo&0x4000, tileInfo&0x2000); - int16_t *ws_paletteAlias=&ws_palette[((tileInfo>>9)&0x0f)<<2]; - - - for (int i=0; iws_fgWindow_x1))) - { - *scanlinePtr=ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; - } - - column++; - scanlinePtr++; - ws_tileRow++; - } - } - } + } + } } else { - // unknown + // render the first clipped tile + if (columnInTile) + { + uint16_t tileInfo = ws_fgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + + int16_t *ws_paletteAlias = &ws_palette[((tileInfo >> 9) & 0x0f) << 2]; + ws_tileRow += columnInTile; + + for (int i = columnInTile ; i < 8 ; i++) + { + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + column++; + scanlinePtr++; + ws_tileRow++; + } + } + + int nbTiles = 28; + + if (columnInTile) + { + nbTiles = 27; + } + + for (int i = 0 ; i < nbTiles ; i++) + { + uint16_t tileInfo = ws_fgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *ws_paletteAlias = &ws_palette[((tileInfo >> 9) & 0x0f) << 2]; + + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + column++; + + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + column++; + + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + column++; + + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + column++; + + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + column++; + + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + column++; + + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + column++; + + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + scanlinePtr++; + ws_tileRow++; + column++; + } + + // render the last clipped tile + if (columnInTile) + { + uint16_t tileInfo = ws_fgScrollRamBase[ws_currentTile & 0x1f]; + ws_currentTile++; + uint8_t *ws_tileRow = ws_tileCache_getTileRow(tileInfo & 0x1ff, lineInTile, tileInfo & 0x8000, + tileInfo & 0x4000, tileInfo & 0x2000); + int16_t *ws_paletteAlias = &ws_palette[((tileInfo >> 9) & 0x0f) << 2]; + + + for (int i = 0 ; i < columnInTile ; i++) + { + if ((*ws_tileRow) && ((column < ws_fgWindow_x0) || (column > ws_fgWindow_x1))) + { + *scanlinePtr = ws_shades[ws_paletteColors[ws_paletteAlias[*ws_tileRow]]]; + } + + column++; + scanlinePtr++; + ws_tileRow++; + } + } } - } + } + else + { + // unknown + } + } + // render sprites + if (ws_ioRam[0x00] & 0x04) + { + int ws_sprWindow_x0 = ws_ioRam[0x0c]; + int ws_sprWindow_y0 = ws_ioRam[0x0d]; + int ws_sprWindow_x1 = ws_ioRam[0x0e]; + int ws_sprWindow_y1 = ws_ioRam[0x0f]; + uint32_t *ws_sprRamBase = (uint32_t *)(internalRam + (((uint32_t)ws_ioRam[0x04]) << 9)); - // render sprites - if (ws_ioRam[0x00]&0x04) - { - int ws_sprWindow_x0=ws_ioRam[0x0c]; - int ws_sprWindow_y0=ws_ioRam[0x0d]; - int ws_sprWindow_x1=ws_ioRam[0x0e]; - int ws_sprWindow_y1=ws_ioRam[0x0f]; - uint32_t *ws_sprRamBase=(uint32_t*)(internalRam+(((uint32_t)ws_ioRam[0x04])<<9)); + // seek to first sprite + ws_sprRamBase += ws_ioRam[0x06] - 1; - // seek to first sprite - ws_sprRamBase+=ws_ioRam[0x06]-1; + for (int i = ws_ioRam[0x06] ; i > ws_ioRam[0x05] ; i--) + { + uint32_t spr = *ws_sprRamBase--; - for (int i=ws_ioRam[0x06]; i>ws_ioRam[0x05]; i--) - { - uint32_t spr=*ws_sprRamBase--; - - if (spr&0x2000) - { - // sprite window on ? - if ((ws_ioRam[0x00]&0x08)&&(spr&0x1000)&&(ws_sprWindow_x0!=ws_sprWindow_x1)) + if (spr & 0x2000) { - ws_drawClippedSpriteLine(framebuffer,ws_gpu_scanline,(spr&0xff000000)>>24, (spr&0x00ff0000)>>16, - spr&0x1ff,8+((spr&0xe00)>>9),spr&0x4000,spr&0x8000, - ws_sprWindow_x0,ws_sprWindow_y0,ws_sprWindow_x1,ws_sprWindow_y1); + // sprite window on ? + if ((ws_ioRam[0x00] & 0x08) && (spr & 0x1000) && (ws_sprWindow_x0 != ws_sprWindow_x1)) + { + ws_drawClippedSpriteLine(framebuffer, ws_gpu_scanline, (spr & 0xff000000) >> 24, + (spr & 0x00ff0000) >> 16, spr & 0x1ff, 8 + ((spr & 0xe00) >> 9), + spr & 0x4000, spr & 0x8000, ws_sprWindow_x0, ws_sprWindow_y0, + ws_sprWindow_x1, ws_sprWindow_y1); + } + else + { + ws_drawClippedSpriteLine(framebuffer, ws_gpu_scanline, (spr & 0xff000000) >> 24, + (spr & 0x00ff0000) >> 16, spr & 0x1ff, 8 + ((spr & 0xe00) >> 9), + spr & 0x4000, spr & 0x8000, + // 0,0,224,144); + 0, 0, 224, 0); + } } - else - { - ws_drawClippedSpriteLine(framebuffer,ws_gpu_scanline,(spr&0xff000000)>>24, (spr&0x00ff0000)>>16, - spr&0x1ff,8+((spr&0xe00)>>9),spr&0x4000,spr&0x8000, - // 0,0,224,144); - 0,0,224,0); - } - } - } - } + } + } } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -1902,58 +1899,60 @@ void ws_gpu_renderScanline(int16_t *framebuffer) //////////////////////////////////////////////////////////////////////////////// void ws_gpu_write_byte(uint32_t offset, uint8_t value) { - // ws 4 color tiles - if ((offset>=0x2000)&&(offset<0x4000)) - { - if (internalRam[offset]!=value) - { - ws_modified_tile[(offset&0x1fff)>>4]=1; - } + // ws 4 color tiles + if ((offset >= 0x2000) && (offset < 0x4000)) + { + if (internalRam[offset] != value) + { + ws_modified_tile[(offset & 0x1fff) >> 4] = 1; + } - // update the ram - internalRam[offset]=value; - } + // update the ram + internalRam[offset] = value; + } - if (ws_gpu_operatingInColor) - { - // wsc 16 color tiles bank 0 - if ((offset>=0x4000)&&(offset<0x8000)) - { - if (internalRam[offset]!=value) - { - wsc_modified_tile[(offset&0x3fff)>>5]=1; - } - } - else - - // wsc 16 color tiles bank 1 - if ((offset>=0x8000)&&(offset<0xC000)) - { - if (internalRam[offset]!=value) + if (ws_gpu_operatingInColor) + { + // wsc 16 color tiles bank 0 + if ((offset >= 0x4000) && (offset < 0x8000)) + { + if (internalRam[offset] != value) { - wsc_modified_tile[512+((offset&0x3fff)>>5)]=1; + wsc_modified_tile[(offset & 0x3fff) >> 5] = 1; } - } + } + else - // update the ram - internalRam[offset]=value; + // wsc 16 color tiles bank 1 + if ((offset >= 0x8000) && (offset < 0xC000)) + { + if (internalRam[offset] != value) + { + wsc_modified_tile[512 + ((offset & 0x3fff) >> 5)] = 1; + } + } - // palette ram - if (offset>=0xfe00) - { - // RGB444 format - uint16_t color=(internalRam[(offset&0xfffe)+1]); - color<<=8; - color|=(internalRam[(offset&0xfffe)]); + // update the ram + internalRam[offset] = value; - wsc_palette[(offset&0x1ff)>>1]=RGB555(((color>>8)&0x0f)<<1,((color>>4)&0x0f)<<1,(color&0x0f)<<1); - } - } - else if (offset<0x4000) - { - internalRam[offset]=value; - } + // palette ram + if (offset >= 0xfe00) + { + // RGB444 format + uint16_t color = (internalRam[(offset & 0xfffe) + 1]); + color <<= 8; + color |= (internalRam[(offset & 0xfffe)]); + + wsc_palette[(offset & 0x1ff) >> 1] = RGB555(((color >> 8) & 0x0f) << 1, ((color >> 4) & 0x0f) << 1, + (color & 0x0f) << 1); + } + } + else if (offset < 0x4000) + { + internalRam[offset] = value; + } } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// diff --git a/source/gpu.h b/source/gpu.h index 6cb51d5..c038e69 100644 --- a/source/gpu.h +++ b/source/gpu.h @@ -1,33 +1,24 @@ -////////////////////////////////////////////////////////////////////////////// -// -////////////////////////////////////////////////////////////////////////////// -// -// -// -// -// -// -////////////////////////////////////////////////////////////////////////////// - +/* + * NewOswan + * gpu.h: + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ #ifndef __GPU_H__ #define __GPU_H__ #include -#define COLOUR_SCHEME_DEFAULT 0 -#define COLOUR_SCHEME_AMBER 1 -#define COLOUR_SCHEME_GREEN 2 - -extern uint8_t ws_gpu_scanline; -extern uint8_t ws_gpu_operatingInColor; -extern uint8_t ws_videoMode; -extern int16_t ws_palette[16*4]; -extern int8_t ws_paletteColors[8]; -extern int16_t wsc_palette[16*16]; -extern unsigned int ws_gpu_unknownPort; - -extern uint32_t vblank_count; +extern uint8_t ws_gpu_scanline; +extern uint8_t ws_gpu_operatingInColor; +extern uint8_t ws_videoMode; +extern int16_t ws_palette[16 * 4]; +extern int8_t ws_paletteColors[8]; +extern int16_t wsc_palette[16 * 16]; +extern unsigned int ws_gpu_unknownPort; +extern uint32_t vblank_count; void ws_gpu_init(void); void ws_gpu_done(void); diff --git a/source/ieeprom.h b/source/ieeprom.h deleted file mode 100644 index 095e25a..0000000 --- a/source/ieeprom.h +++ /dev/null @@ -1,191 +0,0 @@ -////////////////////////////////////////////////////////////////////////////// -// -////////////////////////////////////////////////////////////////////////////// -// -// -// -// -// -// -////////////////////////////////////////////////////////////////////////////// - -uint8_t DefaultBWEEprom[]= -{ - 0xff,0xff,0xff,0xff,0xff,0xff,192,0xff,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,127,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0x00,252,0xff,1,0xff,253,0xff,253,0xff,253,0xff,253, - 0xff,253,0xff,253,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0x00,0x00,3,3,0x00,0x00,0x00,64,128,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 135,5,140,9,5,12,139,12,144,0x00,0x00,2, - 0x00,76,165,0x00,128,0x00,0x00,0x00,0xff,127,0xff,127, - 0xff,127,0xff,127,0xff,127,0xff,127,0xff,127,0xff,127, - 0xff,127,0xff,127,0xff,127,0xff,127,0xff,127,0xff,127, - 0xff,127,0xff,127,0xff,127,0xff,127,0xff,127,0xff,127, - 0xff,127,0xff,127,0xff,127,0xff,127,0xff,127,0xff,127, - 0xff,127,0xff,127,0xff,127,0xff,127,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0x00,0x00,6,6,6,6,6,0x00,0x00,0x00,0x00,0x00, - 1,128,15,0x00,1,1,1,15,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 'W'-54,'O'-54,'N'-54,'D'-54,'E'-54,'R'-54,'S'-54,'W'-54,'A'-54,'N'-54,0x00,0x00,0x00,0x00,0x00, - 0x00,32,1,1,33,1,4,0x00,1, - 0x00,152,60,127,74,1,53,1,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff -}; - - -uint8_t DefaultColorEEprom[]= -{ - 0xff,0xff,0xff,0xff,0xff,0xff,192,0xff,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,127,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0x00,252,0xff,1,0xff,253,0xff,253,0xff,253,0xff,253, - 0xff,253,0xff,253,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0x00,0x00,3,3,0x00,0x00,0x00,64,128,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 135,5,140,9,5,12,139,12,144,0x00,0x00,2, - 0x00,76,165,0x00,128,0x00,0x00,0x00,0xff,127,0xff,127, - 0xff,127,0xff,127,0xff,127,0xff,127,0xff,127,0xff,127, - 0xff,127,0xff,127,0xff,127,0xff,127,0xff,127,0xff,127, - 0xff,127,0xff,127,0xff,127,0xff,127,0xff,127,0xff,127, - 0xff,127,0xff,127,0xff,127,0xff,127,0xff,127,0xff,127, - 0xff,127,0xff,127,0xff,127,0xff,127,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0x00,0x00,6,6,6,6,6,0x00,0x00,0x00,0x00,0x00, - 1,128,15,0x00,1,1,1,15,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 'W'-54,'O'-54,'N'-54,'D'-54,'E'-54,'R'-54,'S'-54,'W'-54,'A'-54,'N'-54,0x00,0x00,0x00,0x00,0x00, - 0x00,32,1,1,33,1,4,0x00,1, - 0x00,152,60,127,74,1,53,1,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff -}; diff --git a/source/io.c b/source/io.c index 65d9f90..106687d 100644 --- a/source/io.c +++ b/source/io.c @@ -1,6 +1,11 @@ -//////////////////////////////////////////////////////////////////////////////// -// I/O ports -//////////////////////////////////////////////////////////////////////////////// +/* + * NewOswan + * io.c: I/O ports implementaton + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ +////////////////////////////////////////////////////////////////////////////// // // // @@ -8,7 +13,6 @@ // // ////////////////////////////////////////////////////////////////////////////// - #include #include #include @@ -40,26 +44,18 @@ extern uint64_t nec_monotonicCycles; enum { - EEPROM_SUBCOMMAND = 0, /* 00 00 */ - EEPROM_WRITE, /* 01 xx */ - EEPROM_READ, /* 10 xx */ - EEPROM_ERASE, /* 11 xx */ - EEPROM_WRITEDISABLE, /* 00 00 */ - EEPROM_WRITEALL, /* 00 01 */ - EEPROM_ERASEALL, /* 00 10 */ - EEPROM_WRITEENABLE /* 00 11 */ + EEPROM_SUBCOMMAND = 0, /* 00 00 */ + EEPROM_WRITE, /* 01 xx */ + EEPROM_READ, /* 10 xx */ + EEPROM_ERASE, /* 11 xx */ + EEPROM_WRITEDISABLE, /* 00 00 */ + EEPROM_WRITEALL, /* 00 01 */ + EEPROM_ERASEALL, /* 00 10 */ + EEPROM_WRITEENABLE /* 00 11 */ }; -char *eii_CommandName[] = -{ - "SUB", - "WRI", - "RED", - "ERA", - "WRD", - "WRA", - "ERL", - "WRE", +char *eii_CommandName[] = { + "SUB", "WRI", "RED", "ERA", "WRD", "WRA", "ERL", "WRE", }; uint8_t iee_WriteEnable = false; @@ -73,7 +69,7 @@ uint16_t cee_Databuffer = 0; uint8_t cee_Mode = EEPROM_READ; -uint8_t *ws_ioRam=NULL; +uint8_t *ws_ioRam = NULL; uint8_t ws_key_start; uint8_t ws_key_x4; @@ -88,7 +84,7 @@ uint8_t ws_key_button_a; uint8_t ws_key_button_b; uint8_t ws_key_flipped; -int rtcDataRegisterReadCount=0; +int rtcDataRegisterReadCount = 0; FILE *ioLogFp = NULL; @@ -105,18 +101,18 @@ FILE *ioLogFp = NULL; //////////////////////////////////////////////////////////////////////////////// void ws_io_reset(void) { - ws_key_start=0; - ws_key_x4=0; - ws_key_x2=0; - ws_key_x1=0; - ws_key_x3=0; - ws_key_y4=0; - ws_key_y2=0; - ws_key_y1=0; - ws_key_y3=0; - ws_key_button_a=0; - ws_key_button_b=0; - int i; + ws_key_start = 0; + ws_key_x4 = 0; + ws_key_x2 = 0; + ws_key_x1 = 0; + ws_key_x3 = 0; + ws_key_y4 = 0; + ws_key_y2 = 0; + ws_key_y1 = 0; + ws_key_y3 = 0; + ws_key_button_a = 0; + ws_key_button_b = 0; + int i; for (i = 0 ; i < 0x100 ; i++) { @@ -133,8 +129,9 @@ void ws_io_reset(void) ws_ioRam[0xC2] = 0xFF; ws_ioRam[0xC3] = 0xFF; - rtcDataRegisterReadCount=0; + rtcDataRegisterReadCount = 0; } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -148,18 +145,19 @@ void ws_io_reset(void) //////////////////////////////////////////////////////////////////////////////// void ws_io_init(void) { - if (ws_ioRam==NULL) - { - ws_ioRam=(uint8_t*)malloc(0x100); - } + if (ws_ioRam == NULL) + { + ws_ioRam = (uint8_t *)malloc(0x100); + } - ws_io_reset(); - ws_key_flipped=0; + ws_io_reset(); + ws_key_flipped = 0; #ifdef IO_DUMP - ioLogFp = fopen("iodump.csv", "wt"); + ioLogFp = fopen("iodump.csv", "wt"); #endif } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -173,8 +171,9 @@ void ws_io_init(void) //////////////////////////////////////////////////////////////////////////////// void ws_io_flipControls(void) { - ws_key_flipped=!ws_key_flipped; + ws_key_flipped = !ws_key_flipped; } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -188,13 +187,13 @@ void ws_io_flipControls(void) //////////////////////////////////////////////////////////////////////////////// void ws_io_done(void) { - if (ws_ioRam==NULL) - { - free(ws_ioRam); - } + if (ws_ioRam == NULL) + { + free(ws_ioRam); + } #ifdef IO_DUMP - fclose(ioLogFp); + fclose(ioLogFp); #endif } @@ -206,134 +205,135 @@ int serialfd = -1; int serial_have_data = 0; unsigned char serial_data = 0; int serial_speed = BDR_9600; + void open_serial() { - if (serialfd < 0) - { - serialfd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY); + if (serialfd < 0) + { + serialfd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY); - //set_baudrate(serial_speed); - serial_have_data = 0; - } + //set_baudrate(serial_speed); + serial_have_data = 0; + } } void set_baudrate(int speed) { - struct termios options; + struct termios options; - if (serialfd < 0) - { - return; - } + if (serialfd < 0) + { + return; + } - tcgetattr(serialfd, &options); + tcgetattr(serialfd, &options); - options.c_cflag &= ~PARENB; - options.c_cflag &= ~CSTOPB; - options.c_cflag &= ~CSIZE; - options.c_cflag |= CS8; + options.c_cflag &= ~PARENB; + options.c_cflag &= ~CSTOPB; + options.c_cflag &= ~CSIZE; + options.c_cflag |= CS8; - if (speed == BDR_9600) - { - cfsetispeed(&options, B9600); - } - else - { - cfsetospeed(&options, B38400); - } + if (speed == BDR_9600) + { + cfsetispeed(&options, B9600); + } + else + { + cfsetospeed(&options, B38400); + } #if 0 - options.c_cflag &= ~CNEW_RTSCTS; + options.c_cflag &= ~CNEW_RTSCTS; #else - options.c_cflag &= ~CRTSCTS; + options.c_cflag &= ~CRTSCTS; #endif - options.c_cflag |= (CLOCAL | CREAD); + options.c_cflag |= (CLOCAL | CREAD); - options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); + options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); - options.c_oflag &= ~OPOST; + options.c_oflag &= ~OPOST; - tcsetattr(serialfd, TCSANOW, &options); + tcsetattr(serialfd, TCSANOW, &options); - /* Make sure read is not blocking */ - fcntl(serialfd, F_SETFL, FNDELAY); + /* Make sure read is not blocking */ + fcntl(serialfd, F_SETFL, FNDELAY); } void close_serial() { - close(serialfd); - serialfd = -1; + close(serialfd); + serialfd = -1; } void check_serial_data() { - unsigned char buf[10]; - int f; + unsigned char buf[10]; + int f; - if (serialfd < 0) - { - return; - } + if (serialfd < 0) + { + return; + } - if (serial_have_data == 0) - { - f = read(serialfd, buf, 1); + if (serial_have_data == 0) + { + f = read(serialfd, buf, 1); - if (f > 0) - { - printf("Ho [%d]!\n", f); - fflush(stdout); - serial_have_data = 0x01; - serial_data = buf[0]; - } - } + if (f > 0) + { + printf("Ho [%d]!\n", f); + fflush(stdout); + serial_have_data = 0x01; + serial_data = buf[0]; + } + } - if(serial_have_data > 0) - { - /* Gen an int if enabled */ - if(ws_ioRam[0xB2] & 0x04) - { - ws_ioRam[0xb6] &= ~ 0x04; - printf("SERIAL INNNNNTTTT!!!!!!!"); - nec_int((ws_ioRam[0xb0]+3)*4); - } - } + if (serial_have_data > 0) + { + /* Gen an int if enabled */ + if (ws_ioRam[0xB2] & 0x04) + { + ws_ioRam[0xb6] &= ~0x04; + printf("SERIAL INNNNNTTTT!!!!!!!"); + nec_int((ws_ioRam[0xb0] + 3) * 4); + } + } } unsigned char read_serial() { - unsigned char buf[10]; - int f; + unsigned char buf[10]; + int f; - if (serialfd < 0) - { - return 0xFF; - } + if (serialfd < 0) + { + return 0xFF; + } - if (serial_have_data > 0) - { - serial_have_data = 0; - return serial_data; - } + if (serial_have_data > 0) + { + serial_have_data = 0; + return serial_data; + } - f = read(serialfd, buf, 1); + f = read(serialfd, buf, 1); - if (f == 1) - { - return buf[0]; - } + if (f == 1) + { + return buf[0]; + } - return 0x42; + return 0x42; } void write_serial(unsigned char value) { - if (serialfd < 0) - { - return; - } + if (serialfd < 0) + { + return; + } - write(serialfd, &value, 1); + write(serialfd, &value, 1); } @@ -390,10 +390,12 @@ uint8_t cpu_readport(uint8_t port) case 0x91: case 0x92: case 0x93: - case 0x94:retVal = ws_audio_port_read(port); + case 0x94: + retVal = ws_audio_port_read(port); break; - case 0xb5:w1 = ws_ioRam[0xb5]; + case 0xb5: + w1 = ws_ioRam[0xb5]; if (w1 & 0x40) { @@ -445,10 +447,12 @@ uint8_t cpu_readport(uint8_t port) { case WS_SYSTEM_AUTODETECT: case WS_SYSTEM_MONO: - case WS_SYSTEM_COLOR:retVal = 0x00; + case WS_SYSTEM_COLOR: + retVal = 0x00; break; - case WS_SYSTEM_CRYSTAL:retVal = 0x80; + case WS_SYSTEM_CRYSTAL: + retVal = 0x80; break; } break; @@ -509,31 +513,38 @@ uint8_t cpu_readport(uint8_t port) switch (rtcDataRegisterReadCount) { - case 0:rtcDataRegisterReadCount++; + case 0: + rtcDataRegisterReadCount++; retVal = BCD(newtime->tm_year - 100); goto exit; - case 1:rtcDataRegisterReadCount++; + case 1: + rtcDataRegisterReadCount++; retVal = BCD(newtime->tm_mon); goto exit; - case 2:rtcDataRegisterReadCount++; + case 2: + rtcDataRegisterReadCount++; retVal = BCD(newtime->tm_mday); goto exit; - case 3:rtcDataRegisterReadCount++; + case 3: + rtcDataRegisterReadCount++; retVal = BCD(newtime->tm_wday); goto exit; - case 4:rtcDataRegisterReadCount++; + case 4: + rtcDataRegisterReadCount++; retVal = BCD(newtime->tm_hour); goto exit; - case 5:rtcDataRegisterReadCount++; + case 5: + rtcDataRegisterReadCount++; retVal = BCD(newtime->tm_min); goto exit; - case 6:rtcDataRegisterReadCount = 0; + case 6: + rtcDataRegisterReadCount = 0; retVal = BCD(newtime->tm_sec); goto exit; } @@ -547,15 +558,18 @@ uint8_t cpu_readport(uint8_t port) goto exit; } - case 0xD0:retVal = 0; + case 0xD0: + retVal = 0; goto exit; /* Serial port link.. */ - case 0xB1:retVal = read_serial(); + case 0xB1: + retVal = read_serial(); printf("RS232: Read %02X\n", retVal); goto exit; - case 0xB3:check_serial_data(); + case 0xB3: + check_serial_data(); if (ws_ioRam[0xB3] & 0x80) { @@ -572,10 +586,12 @@ uint8_t cpu_readport(uint8_t port) goto exit; case 0xCC: - case 0xCD:retVal = 0; + case 0xCD: + retVal = 0; break; - default:retVal = ws_ioRam[port]; + default: + retVal = ws_ioRam[port]; if (port > 0xD0) { printf("ReadIO %02X <= %02X\n", port, retVal); @@ -586,7 +602,8 @@ uint8_t cpu_readport(uint8_t port) case 0xAA: case 0xAB: case 0xAC: - case 0xAD:retVal = ws_gpu_port_read(port); + case 0xAD: + retVal = ws_gpu_port_read(port); break; } @@ -645,7 +662,8 @@ void cpu_writeport(uint32_t port, uint8_t value) switch (port) { /* GPU IOs */ - case 0x00:break; + case 0x00: + break; case 0x04: case 0x07: @@ -666,7 +684,8 @@ void cpu_writeport(uint32_t port, uint8_t value) case 0x11: case 0x12: case 0x13: - case 0x14:break; + case 0x14: + break; case 0x15: printf("Icons %c %c %c %c %c %c %c %c\n", (value >> 7) & 1 ? '?' : ' ', (value >> 6) & 1 ? '?' : ' ', @@ -708,7 +727,8 @@ void cpu_writeport(uint32_t port, uint8_t value) case 0x36: case 0x24: case 0x2E: - case 0x37:break; + case 0x37: + break; /* DMAs */ case 0x40: @@ -718,7 +738,8 @@ void cpu_writeport(uint32_t port, uint8_t value) case 0x44: case 0x45: case 0x46: - case 0x47:break; + case 0x47: + break; case 0x48: // DMA @@ -753,14 +774,17 @@ void cpu_writeport(uint32_t port, uint8_t value) case 0x4c: case 0x4d: case 0x4e: - case 0x4f:ws_audio_port_write(port, value); + case 0x4f: + ws_audio_port_write(port, value); break; /* DMA Start! */ - case 0x52:break; + case 0x52: + break; /* GPU (again) */ - case 0x60:break; + case 0x60: + break; /* System */ case 0x62: printf("HeyHo!"); @@ -797,7 +821,8 @@ void cpu_writeport(uint32_t port, uint8_t value) case 0x9B: case 0x9C: case 0x9D: - case 0x9E:ws_audio_port_write(port, value); + case 0x9E: + ws_audio_port_write(port, value); break; /* Hardware */ @@ -815,17 +840,20 @@ void cpu_writeport(uint32_t port, uint8_t value) case 0xA8: case 0xA9: case 0xAA: - case 0xAB:break; + case 0xAB: + break; /* Intc */ case 0xB0: case 0xB2: case 0xB4: - case 0xB6:break; + case 0xB6: + break; /* Serial */ - case 0xB1:write_serial(value); + case 0xB1: + write_serial(value); break; case 0xB3: @@ -850,7 +878,8 @@ void cpu_writeport(uint32_t port, uint8_t value) break; /* buttons */ - case 0xB5:break; + case 0xB5: + break; /* Internal EEPROM */ case 0xba: /* DATA Low */ @@ -885,216 +914,216 @@ void cpu_writeport(uint32_t port, uint8_t value) command = (iee_SelAddress >> 10) & 0x3; address = iee_SelAddress & 0x3FF; subcmd = (iee_SelAddress >> 8) & 0x03; - } - else - { + } + else + { /* S CC aaAAAA */ command = (iee_SelAddress >> 6) & 0x3; address = iee_SelAddress & 0x3F; subcmd = (iee_SelAddress >> 4) & 0x03; - } - + } - if (command == EEPROM_SUBCOMMAND) - { + + if (command == EEPROM_SUBCOMMAND) + { command = EEPROM_WRITEDISABLE + subcmd; - } + } - printf("IEEP: RA:%04X RD:%04X A:%03X C:%s", iee_SelAddress, iee_Databuffer, address, eii_CommandName[command]); + printf("IEEP: RA:%04X RD:%04X A:%03X C:%s", iee_SelAddress, iee_Databuffer, address, eii_CommandName[command]); - if (value & 0x40) - { + if (value & 0x40) + { /* Sub command */ printf(" - Sub"); if (command == EEPROM_WRITEENABLE) { - printf(" Write Enable\n"); - iee_WriteEnable = true; + printf(" Write Enable\n"); + iee_WriteEnable = true; } else if (command == EEPROM_WRITEDISABLE) { - printf(" Write Disable\n"); - iee_WriteEnable = false; + printf(" Write Disable\n"); + iee_WriteEnable = false; } else if (command == EEPROM_ERASEALL) { - printf(" Erase All\n"); - if (ws_gpu_operatingInColor) - { - memset(internalEeprom, 0, COLOR_IEEPROM_SIZE); - } - else - { - memset(internalEeprom, 0, BW_IEEPROM_SIZE); - } + printf(" Erase All\n"); + if (ws_gpu_operatingInColor) + { + memset(internalEeprom, 0, COLOR_IEEPROM_SIZE); + } + else + { + memset(internalEeprom, 0, BW_IEEPROM_SIZE); + } } else { - printf(" Write All?\n"); + printf(" Write All?\n"); } - } - else if (value & 0x20) - { + } + else if (value & 0x20) + { /* Write */ printf(" - Write?"); if (iee_WriteEnable) { - printf(" Yes : %04X\n", iee_Databuffer); - internalEeprom[address] = iee_Databuffer; + printf(" Yes : %04X\n", iee_Databuffer); + internalEeprom[address] = iee_Databuffer; } else { - printf(" No\n"); + printf(" No\n"); } - } - else if (value & 0x10) - { + } + else if (value & 0x10) + { /* Read */ printf(" - Read"); iee_Databuffer = internalEeprom[address]; printf(" Data : %04X\n", iee_Databuffer); - } - else - { + } + else + { printf(" Unknown value: %02X\n", value); - } - fflush(stdout); - } - break; + } + fflush(stdout); + } + break; - /* MBC */ - case 0xC0: - case 0xC1: - case 0xC2: - case 0xC3: - break; + /* MBC */ + case 0xC0: + case 0xC1: + case 0xC2: + case 0xC3: + break; - /* Cart EEPROM */ - case 0xC4: /* Data High */ - cee_Databuffer = cee_Databuffer & 0xFF00; - cee_Databuffer = cee_Databuffer | (value); - break; + /* Cart EEPROM */ + case 0xC4: /* Data High */ + cee_Databuffer = cee_Databuffer & 0xFF00; + cee_Databuffer = cee_Databuffer | (value); + break; - case 0xC5: /* Data High */ - cee_Databuffer = cee_Databuffer & 0x00FF; - cee_Databuffer = cee_Databuffer | (value << 8); - break; + case 0xC5: /* Data High */ + cee_Databuffer = cee_Databuffer & 0x00FF; + cee_Databuffer = cee_Databuffer | (value << 8); + break; - case 0xC6: /* Address Low */ - case 0xC7: /* Address High */ - break; + case 0xC6: /* Address Low */ + case 0xC7: /* Address High */ + break; - case 0xC8: /* Command / Status */ - { - uint16_t address, command, subcmd; /*, start;*/ + case 0xC8: /* Command / Status */ + { + uint16_t address, command, subcmd; /*, start;*/ - cee_SelAddress = (ws_ioRam[0xBD] << 8) | ws_ioRam[0xBC]; + cee_SelAddress = (ws_ioRam[0xBD] << 8) | ws_ioRam[0xBC]; - /* S CC aaAAAA */ - command = (cee_SelAddress >> 6) & 0x3; - address = cee_SelAddress & 0x3F; - subcmd = (cee_SelAddress >> 4) & 0x03; + /* S CC aaAAAA */ + command = (cee_SelAddress >> 6) & 0x3; + address = cee_SelAddress & 0x3F; + subcmd = (cee_SelAddress >> 4) & 0x03; - if (command == EEPROM_SUBCOMMAND) - { - command = EEPROM_WRITEDISABLE + subcmd; - } + if (command == EEPROM_SUBCOMMAND) + { + command = EEPROM_WRITEDISABLE + subcmd; + } - printf("CEEP: RA:%04X RD:%04X A:%03X C:%s", cee_SelAddress, cee_Databuffer, address, eii_CommandName[command]); + printf("CEEP: RA:%04X RD:%04X A:%03X C:%s", cee_SelAddress, cee_Databuffer, address, eii_CommandName[command]); - if (value & 0x40) - { - /* Sub command */ - printf(" - Sub"); - if (command == EEPROM_WRITEENABLE) - { - printf(" Write Enable\n"); - cee_WriteEnable = true; - } - else if (command == EEPROM_WRITEDISABLE) - { - printf(" Write Disable\n"); - cee_WriteEnable = false; - } - else if (command == EEPROM_ERASEALL) - { - printf(" Erase All\n"); - /* Nothing here at the moment */ - } - else - { - printf(" Write All?\n"); - } - } - else if (value & 0x20) - { - /* Write */ - printf(" - Write?"); - if (cee_WriteEnable) - { - printf(" Yes : %04X\n", cee_Databuffer); - externalEeprom[address] = cee_Databuffer; - } - else - { - printf(" No\n"); - } - } - else if (value & 0x10) - { - /* Read */ - printf(" - Read"); - cee_Databuffer = externalEeprom[address]; - printf(" Data : %04X\n", cee_Databuffer); - } - else - { - printf(" Unknown value: %02X\n", value); - } - fflush(stdout); - } - break; + if (value & 0x40) + { + /* Sub command */ + printf(" - Sub"); + if (command == EEPROM_WRITEENABLE) + { + printf(" Write Enable\n"); + cee_WriteEnable = true; + } + else if (command == EEPROM_WRITEDISABLE) + { + printf(" Write Disable\n"); + cee_WriteEnable = false; + } + else if (command == EEPROM_ERASEALL) + { + printf(" Erase All\n"); + /* Nothing here at the moment */ + } + else + { + printf(" Write All?\n"); + } + } + else if (value & 0x20) + { + /* Write */ + printf(" - Write?"); + if (cee_WriteEnable) + { + printf(" Yes : %04X\n", cee_Databuffer); + externalEeprom[address] = cee_Databuffer; + } + else + { + printf(" No\n"); + } + } + else if (value & 0x10) + { + /* Read */ + printf(" - Read"); + cee_Databuffer = externalEeprom[address]; + printf(" Data : %04X\n", cee_Databuffer); + } + else + { + printf(" Unknown value: %02X\n", value); + } + fflush(stdout); + } + break; - case 0xca: - if(value==0x15) - { - rtcDataRegisterReadCount=0; - } + case 0xca: + if (value == 0x15) + { + rtcDataRegisterReadCount = 0; + } - break; - break; + break; + break; - case 0xCB: - break; + case 0xCB: + break; - case 0xF0: - break; + case 0xF0: + break; - case 0xF1: - printf("%d\n", (signed short)((value << 8) | ws_ioRam[0xF0])); - break; + case 0xF1: + printf("%d\n", (signed short)((value << 8) | ws_ioRam[0xF0])); + break; - case 0xF2: - printf("%c", value); - fflush(stdout); - break; + case 0xF2: + printf("%c", value); + fflush(stdout); + break; - case 0xB7: - break; /* Somwthing to write there, but what? */ + case 0xB7: + break; /* Somwthing to write there, but what? */ - default: - unknown_io_port=1; - } + default: + unknown_io_port = 1; + } - if ((ws_gpu_port_write(port,value) == 1) && (unknown_io_port == 1)) - { - fprintf(log_get(),"WriteIO(%02X, %02X) [%04X:%04Xh];\n",port, value, I.sregs[CS], I.ip); - } + if ((ws_gpu_port_write(port, value) == 1) && (unknown_io_port == 1)) + { + fprintf(log_get(), "WriteIO(%02X, %02X) [%04X:%04Xh];\n", port, value, I.sregs[CS], I.ip); + } - if (port >= 0xC4) - { - fprintf(log_get(),"WriteMBCIO(%02X, %02X);\n",port, value); - } + if (port >= 0xC4) + { + fprintf(log_get(), "WriteMBCIO(%02X, %02X);\n", port, value); + } } diff --git a/source/io.h b/source/io.h index 8968e17..5754ba6 100644 --- a/source/io.h +++ b/source/io.h @@ -1,5 +1,10 @@ -////////////////////////////////////////////////////////////////////////////// -// +/* + * NewOswan + * io.h: + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ ////////////////////////////////////////////////////////////////////////////// // // @@ -14,7 +19,7 @@ #include -extern uint8_t *ws_ioRam; +extern uint8_t *ws_ioRam; extern uint8_t ws_key_start; extern uint8_t ws_key_x4; extern uint8_t ws_key_x2; diff --git a/source/log.c b/source/log.c index f9a0fae..0ad170e 100644 --- a/source/log.c +++ b/source/log.c @@ -1,5 +1,10 @@ -////////////////////////////////////////////////////////////////////////////// -// +/* + * NewOswan + * log.c: + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ ////////////////////////////////////////////////////////////////////////////// // // @@ -13,7 +18,7 @@ #include #include "log.h" -FILE *log_stream=NULL; +FILE *log_stream = NULL; ////////////////////////////////////////////////////////////////////////////// // @@ -27,16 +32,17 @@ FILE *log_stream=NULL; ////////////////////////////////////////////////////////////////////////////// int log_init(char *path) { - //log_stream=fopen(path,"wrt"); - log_stream = stdout; + //log_stream=fopen(path,"wrt"); + log_stream = stdout; - if (log_stream==NULL) - { - return(0); - } + if (log_stream == NULL) + { + return (0); + } - return(1); + return (1); } + ////////////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////////// @@ -49,8 +55,9 @@ int log_init(char *path) ////////////////////////////////////////////////////////////////////////////// FILE *log_get(void) { - return(log_stream); + return (log_stream); } + ////////////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////////// @@ -63,5 +70,5 @@ FILE *log_get(void) ////////////////////////////////////////////////////////////////////////////// void log_done(void) { - fclose(log_stream); + fclose(log_stream); } diff --git a/source/log.h b/source/log.h index b439ccc..14b6941 100644 --- a/source/log.h +++ b/source/log.h @@ -1,5 +1,10 @@ -////////////////////////////////////////////////////////////////////////////// -// +/* + * NewOswan + * log.h: + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ ////////////////////////////////////////////////////////////////////////////// // // diff --git a/source/memory.c b/source/memory.c index 0a3c3f6..2bfd545 100644 --- a/source/memory.c +++ b/source/memory.c @@ -1,5 +1,10 @@ -//////////////////////////////////////////////////////////////////////////////// -// Memory +/* + * NewOswan + * memory.c: Memory implementatoion + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ //////////////////////////////////////////////////////////////////////////////// // Notes: need to optimize cpu_writemem20 // @@ -16,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -29,7 +35,6 @@ #include "gpu.h" #include "audio.h" #include "memory.h" -#include //////////////////////////////////////////////////////////////////////////////// // @@ -42,7 +47,7 @@ // // //////////////////////////////////////////////////////////////////////////////// -#define IO_ROM_BANK_BASE_SELECTOR 0xC0 +#define IO_ROM_BANK_BASE_SELECTOR 0xC0 uint8_t *ws_rom; uint8_t *ws_staticRam; @@ -60,15 +65,16 @@ uint16_t *internalEeprom; extern uint8_t *ws_ioRam; -uint16_t ws_rom_checksum; +uint16_t ws_rom_checksum; -uint8_t ws_haveColorIRom; -uint8_t ws_haveBWIRom; +uint8_t ws_haveCrystalIRom; +uint8_t ws_haveColorIRom; +uint8_t ws_haveBWIRom; -uint32_t sramAddressMask; -uint32_t externalEepromAddressMask; -uint32_t romAddressMask; -uint32_t romSize; +uint32_t sramAddressMask; +uint32_t externalEepromAddressMask; +uint32_t romAddressMask; +uint32_t romSize; int ws_sram_dirty = 0; @@ -76,46 +82,46 @@ extern nec_Regs I; void dump_memory() { - int i; - FILE *fp; - printf("Dumping memory....\n"); - fp = fopen("iram.bin", "wb"); - fwrite(internalRam, 1, 0x10000, fp); - fclose(fp); + int i; + FILE *fp; + printf("Dumping memory....\n"); + fp = fopen("iram.bin", "wb"); + fwrite(internalRam, 1, 0x10000, fp); + fclose(fp); - fp = fopen("sram.bin", "wb"); - fwrite(ws_staticRam, 1, 0x10000, fp); - fclose(fp); + fp = fopen("sram.bin", "wb"); + fwrite(ws_staticRam, 1, 0x10000, fp); + fclose(fp); - fp = fopen("rom.bin", "wb"); - fwrite(ws_rom, 1, romSize, fp); - fclose(fp); + fp = fopen("rom.bin", "wb"); + fwrite(ws_rom, 1, romSize, fp); + fclose(fp); - fp = fopen("memorydump.bin", "wb"); - fwrite(internalRam, 1, 0x10000, fp); - /* page 1 */ - fwrite(&(ws_staticRam[0 & sramAddressMask]), 1, 0x10000, fp); - fwrite(&(ws_rom[((ws_ioRam[IO_ROM_BANK_BASE_SELECTOR+2]&((romSize>>16)-1))<<16)]), 1, 0x10000, fp); - fwrite(&(ws_rom[((ws_ioRam[IO_ROM_BANK_BASE_SELECTOR+3]&((romSize>>16)-1))<<16)]), 1, 0x10000, fp); + fp = fopen("memorydump.bin", "wb"); + fwrite(internalRam, 1, 0x10000, fp); + /* page 1 */ + fwrite(&(ws_staticRam[0 & sramAddressMask]), 1, 0x10000, fp); + fwrite(&(ws_rom[((ws_ioRam[IO_ROM_BANK_BASE_SELECTOR + 2] & ((romSize >> 16) - 1)) << 16)]), 1, 0x10000, fp); + fwrite(&(ws_rom[((ws_ioRam[IO_ROM_BANK_BASE_SELECTOR + 3] & ((romSize >> 16) - 1)) << 16)]), 1, 0x10000, fp); - for(i = 4; i < 0x10; i++) - { - int romBank=(256-(((ws_ioRam[IO_ROM_BANK_BASE_SELECTOR]&0xf)<<4)|(i&0xf))); - fwrite(&(ws_rom[(unsigned)(romSize-(romBank<<16))]), 1, 0x10000, fp); - } + for (i = 4 ; i < 0x10 ; i++) + { + int romBank = (256 - (((ws_ioRam[IO_ROM_BANK_BASE_SELECTOR] & 0xf) << 4) | (i & 0xf))); + fwrite(&(ws_rom[(unsigned)(romSize - (romBank << 16))]), 1, 0x10000, fp); + } - fclose(fp); + fclose(fp); - fp = fopen("registers.bin", "wb"); - fwrite(ws_ioRam, 1, 256, fp); - fclose(fp); + fp = fopen("registers.bin", "wb"); + fwrite(ws_ioRam, 1, 256, fp); + fclose(fp); - fp = fopen("cpuregs.bin", "wb"); - /* CS */ - fwrite(&I.sregs[CS], 1, 2, fp); - /* IP */ - fwrite(&I.ip, 1, 2, fp); - fclose(fp); + fp = fopen("cpuregs.bin", "wb"); + /* CS */ + fwrite(&I.sregs[CS], 1, 2, fp); + /* IP */ + fwrite(&I.ip, 1, 2, fp); + fclose(fp); } //////////////////////////////////////////////////////////////////////////////// @@ -131,24 +137,25 @@ void dump_memory() //////////////////////////////////////////////////////////////////////////////// void cpu_writemem20(uint32_t addr, uint8_t value) { - uint32_t offset=addr&0xffff; - uint32_t bank=addr>>16; + uint32_t offset = addr & 0xffff; + uint32_t bank = addr >> 16; - if (!bank) - { - // 0 - RAM - 16 KB (WS) / 64 KB (WSC) internal RAM - ws_gpu_write_byte(offset,value); - ws_audio_write_byte(offset,value); - } - else if (bank==1) - { + if (!bank) + { + // 0 - RAM - 16 KB (WS) / 64 KB (WSC) internal RAM + ws_gpu_write_byte(offset, value); + ws_audio_write_byte(offset, value); + } + else if (bank == 1) + { // 1 - SRAM (cart) - ws_staticRam[offset&sramAddressMask]=value; - ws_sram_dirty = 1; - } + ws_staticRam[offset & sramAddressMask] = value; + ws_sram_dirty = 1; + } - // other banks are read-only + // other banks are read-only } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -206,7 +213,8 @@ uint8_t cpu_readmem20(uint32_t addr) temp &= (romSize - 1); return ws_rom[temp]; - case 0xF:hwReg = ws_ioRam[0xA0]; + case 0xF: + hwReg = ws_ioRam[0xA0]; if (!(hwReg & 1)) { @@ -237,7 +245,8 @@ uint8_t cpu_readmem20(uint32_t addr) } // fall through - default:hwReg = ws_ioRam[0xC0]; + default: + hwReg = ws_ioRam[0xC0]; temp = hwReg << 20; temp += addr & 0xFFFFF; temp &= (romSize - 1); @@ -246,6 +255,7 @@ uint8_t cpu_readmem20(uint32_t addr) return (0x90); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -267,9 +277,9 @@ char *load_file(char *filename) fstat(fd, &FileStat); - printf("Trying to load %s, size = %lu...\n",filename, (unsigned long)FileStat.st_size); + printf("Trying to load %s, size = %lu...\n", filename, (unsigned long)FileStat.st_size); - ret_ptr = (char *)mmap(NULL, FileStat.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + ret_ptr = (char *)mmap(NULL, FileStat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); close(fd); @@ -280,6 +290,7 @@ char *load_file(char *filename) return ret_ptr; } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -296,9 +307,9 @@ char *create_file(char *filename, uint32_t size) int fd; uint32_t i; char *ret_ptr; - char buf[] = { 0 }; + char buf[] = {0}; - printf("Trying to create %s, size = %u...\n",filename, size); + printf("Trying to create %s, size = %u...\n", filename, size); fd = open(filename, O_CREAT | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_TRUNC, 0644); fchmod(fd, 0644); close(fd); @@ -306,16 +317,16 @@ char *create_file(char *filename, uint32_t size) fd = open(filename, O_RDWR); - for(i = 0; i < size; i++) + for (i = 0 ; i < size ; i++) { - write(fd, buf, 1); + write(fd, buf, 1); } close(fd); - sync(); + sync(); fd = open(filename, O_RDWR); - ret_ptr = (char *)mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + ret_ptr = (char *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); close(fd); @@ -326,6 +337,7 @@ char *create_file(char *filename, uint32_t size) return ret_ptr; } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -403,6 +415,7 @@ void ws_memory_init(uint8_t *rom, uint32_t wsRomSize) romAddressMask = romSize - 1; } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -416,8 +429,9 @@ void ws_memory_init(uint8_t *rom, uint32_t wsRomSize) //////////////////////////////////////////////////////////////////////////////// void ws_memory_reset(void) { - memset(internalRam,0,0x10000); + memset(internalRam, 0, 0x10000); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -431,8 +445,9 @@ void ws_memory_reset(void) //////////////////////////////////////////////////////////////////////////////// void ws_memory_done(void) { - free(internalRam); + free(internalRam); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -446,8 +461,9 @@ void ws_memory_done(void) //////////////////////////////////////////////////////////////////////////////// uint8_t *memory_getRom(void) { - return(ws_rom); + return (ws_rom); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -459,10 +475,11 @@ uint8_t *memory_getRom(void) // // //////////////////////////////////////////////////////////////////////////////// -uint32_t memory_getRomSize(void) +uint32_t memory_getRomSize(void) { - return(romSize); + return (romSize); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -474,7 +491,7 @@ uint32_t memory_getRomSize(void) // // //////////////////////////////////////////////////////////////////////////////// -uint16_t memory_getRomCrc(void) +uint16_t memory_getRomCrc(void) { - return(ws_rom_checksum); + return (ws_rom_checksum); } diff --git a/source/memory.h b/source/memory.h index 8694ca8..f33a80f 100644 --- a/source/memory.h +++ b/source/memory.h @@ -1,5 +1,10 @@ -////////////////////////////////////////////////////////////////////////////// -// +/* + * NewOswan + * memory.h: + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ ////////////////////////////////////////////////////////////////////////////// // // @@ -14,16 +19,16 @@ #include -extern uint8_t *ws_staticRam; -extern uint8_t *internalRam; -extern uint8_t *externalEeprom; +extern uint8_t *ws_staticRam; +extern uint8_t *internalRam; +extern uint8_t *externalEeprom; -void ws_memory_init(uint8_t *rom, uint32_t romSize); -void ws_memory_reset(void); -uint8_t *memory_getRom(void); -uint32_t memory_getRomSize(void); -uint16_t memory_getRomCrc(void); -void ws_memory_done(void); +void ws_memory_init(uint8_t *rom, uint32_t romSize); +void ws_memory_reset(void); +uint8_t *memory_getRom(void); +uint32_t memory_getRomSize(void); +uint16_t memory_getRomCrc(void); +void ws_memory_done(void); void memory_load(int fp); void memory_save(int fp); diff --git a/source/nec/nec.c b/source/nec/nec.c index 1f4b76f..dd1516f 100644 --- a/source/nec/nec.c +++ b/source/nec/nec.c @@ -1,3 +1,10 @@ +/* + * NewOswan + * nec.c: + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ /**************************************************************************** NEC V30MZ(V20/V30/V33) emulator @@ -26,7 +33,6 @@ ****************************************************************************/ - #include #include #include @@ -44,8 +50,8 @@ int nec_ICount; nec_Regs I; static uint32_t cpu_type; -static uint32_t prefix_base; /* base address of the latest prefix segment */ -char seg_prefix; /* prefix segment indicator */ +static uint32_t prefix_base; /* base address of the latest prefix segment */ +char seg_prefix; /* prefix segment indicator */ /* The interrupt number of a pending external interrupt pending NMI is 2. */ @@ -61,47 +67,49 @@ static uint8_t parity_table[256]; /***************************************************************************/ -void nec_reset (void *param) +void nec_reset(void *param) { - unsigned int i,j,c; - BREGS reg_name[8]= { AL, CL, DL, BL, AH, CH, DH, BH }; + unsigned int i, j, c; + BREGS reg_name[8] = {AL, CL, DL, BL, AH, CH, DH, BH}; - nec_monotonicCycles = 0; + nec_monotonicCycles = 0; - memset( &I, 0, sizeof(I) ); + memset(&I, 0, sizeof(I)); - no_interrupt=0; - I.sregs[CS] = 0xffff; + no_interrupt = 0; + I.sregs[CS] = 0xffff; - for (i = 0; i < 256; i++) - { - for (j = i, c = 0; j > 0; j >>= 1) - if (j & 1) - { - c++; - } + for (i = 0 ; i < 256 ; i++) + { + for (j = i, c = 0 ; j > 0 ; j >>= 1) + { + if (j & 1) + { + c++; + } + } - parity_table[i] = !(c & 1); - } + parity_table[i] = !(c & 1); + } - I.ZeroVal = I.ParityVal = 1; - SetMD(1); /* set the mode-flag = native mode */ + I.ZeroVal = I.ParityVal = 1; + SetMD(1); /* set the mode-flag = native mode */ - for (i = 0; i < 256; i++) - { - Mod_RM.reg.b[i] = reg_name[(i & 0x38) >> 3]; - Mod_RM.reg.w[i] = (WREGS) ( (i & 0x38) >> 3) ; - } + for (i = 0 ; i < 256 ; i++) + { + Mod_RM.reg.b[i] = reg_name[(i & 0x38) >> 3]; + Mod_RM.reg.w[i] = (WREGS)((i & 0x38) >> 3); + } - for (i = 0xc0; i < 0x100; i++) - { - Mod_RM.RM.w[i] = (WREGS)( i & 7 ); - Mod_RM.RM.b[i] = (BREGS)reg_name[i & 7]; - } + for (i = 0xc0 ; i < 0x100 ; i++) + { + Mod_RM.RM.w[i] = (WREGS)(i & 7); + Mod_RM.RM.b[i] = (BREGS)reg_name[i & 7]; + } } -void nec_exit (void) +void nec_exit(void) { } @@ -111,57 +119,57 @@ void dump_memory(); void nec_int(uint16_t vector) { - uint32_t dest_seg, dest_off; + uint32_t dest_seg, dest_off; - if(I.IF) - { - i_pushf(); - I.TF = I.IF = 0; - dest_off = ReadWord(vector); - dest_seg = ReadWord(vector+2); + if (I.IF) + { + i_pushf(); + I.TF = I.IF = 0; + dest_off = ReadWord(vector); + dest_seg = ReadWord(vector + 2); - if ((dest_off == 0) && (dest_seg == 0)) - { - printf("Something wrong with the interrupt, exiting...\n"); - //dump_memory(); - return; - } + if ((dest_off == 0) && (dest_seg == 0)) + { + printf("Something wrong with the interrupt, exiting...\n"); + //dump_memory(); + return; + } - PUSH(I.sregs[CS]); - PUSH(I.ip); - I.ip = (int16_t)dest_off; - I.sregs[CS] = (int16_t)dest_seg; - } + PUSH(I.sregs[CS]); + PUSH(I.ip); + I.ip = (int16_t)dest_off; + I.sregs[CS] = (int16_t)dest_seg; + } } static void nec_interrupt(uint32_t int_num, /*BOOLEAN*/ int md_flag) { - uint32_t dest_seg, dest_off; + uint32_t dest_seg, dest_off; - if (int_num == UINT32_MAX) - { - return; - } + if (int_num == UINT32_MAX) + { + return; + } - i_pushf(); - I.TF = I.IF = 0; + i_pushf(); + I.TF = I.IF = 0; - dest_off = ReadWord((int_num)*4); - dest_seg = ReadWord((int_num)*4+2); + dest_off = ReadWord((int_num) * 4); + dest_seg = ReadWord((int_num) * 4 + 2); - if ((dest_off == 0) && (dest_seg == 0)) - { - printf("Something wrong with the interrupt, exiting...\n"); - //dump_memory(); - return; - } + if ((dest_off == 0) && (dest_seg == 0)) + { + printf("Something wrong with the interrupt, exiting...\n"); + //dump_memory(); + return; + } - PUSH(I.sregs[CS]); - PUSH(I.ip); - I.ip = (int16_t)dest_off; - I.sregs[CS] = (int16_t)dest_seg; + PUSH(I.sregs[CS]); + PUSH(I.ip); + I.ip = (int16_t)dest_off; + I.sregs[CS] = (int16_t)dest_seg; } @@ -170,3594 +178,3902 @@ static void nec_interrupt(uint32_t int_num, /*BOOLEAN*/ int md_flag) /* OPCODES */ /****************************************************************************/ -#define OP(num,func_name) static void func_name(void) +#define OP(num, func_name) static void func_name(void) -OP( 0x00, i_add_br8 ) +OP(0x00, i_add_br8) { - DEF_br8; - ADDB; - PutbackRMByte(ModRM,dst); - CLKM(3,1); -} -OP( 0x01, i_add_wr16 ) -{ - DEF_wr16; - ADDW; - PutbackRMWord(ModRM,dst); - CLKM(3,1); -} -OP( 0x02, i_add_r8b ) -{ - DEF_r8b; - ADDB; - RegByte(ModRM)=dst; - CLKM(2,1); -} -OP( 0x03, i_add_r16w ) -{ - DEF_r16w; - ADDW; - RegWord(ModRM)=dst; - CLKM(2,1); -} -OP( 0x04, i_add_ald8 ) -{ - DEF_ald8; - ADDB; - I.regs.b[AL]=dst; - CLK(1); -} -OP( 0x05, i_add_axd16) -{ - DEF_axd16; - ADDW; - I.regs.w[AW]=dst; - CLK(1); -} -OP( 0x06, i_push_es ) -{ - PUSH(I.sregs[ES]); - CLK(2); -} -OP( 0x07, i_pop_es ) -{ - POP(I.sregs[ES]); - CLK(3); + DEF_br8; + ADDB; + PutbackRMByte(ModRM, dst); + CLKM(3, 1); } -OP( 0x08, i_or_br8 ) +OP(0x01, i_add_wr16) { - DEF_br8; - ORB; - PutbackRMByte(ModRM,dst); - CLKM(3,1); -} -OP( 0x09, i_or_wr16 ) -{ - DEF_wr16; - ORW; - PutbackRMWord(ModRM,dst); - CLKM(3,1); -} -OP( 0x0a, i_or_r8b ) -{ - DEF_r8b; - ORB; - RegByte(ModRM)=dst; - CLKM(2,1); -} -OP( 0x0b, i_or_r16w ) -{ - DEF_r16w; - ORW; - RegWord(ModRM)=dst; - CLKM(2,1); -} -OP( 0x0c, i_or_ald8 ) -{ - DEF_ald8; - ORB; - I.regs.b[AL]=dst; - CLK(1); -} -OP( 0x0d, i_or_axd16 ) -{ - DEF_axd16; - ORW; - I.regs.w[AW]=dst; - CLK(1); -} -OP( 0x0e, i_push_cs ) -{ - PUSH(I.sregs[CS]); - CLK(2); -} -OP( 0x0f, i_pre_nec ) -{ - uint32_t ModRM, tmp, tmp2; /* pop cs at V30MZ? */ - - switch (FETCH) - { - case 0x10 : - BITOP_BYTE; - CLKS(3,3,4); - tmp2 = I.regs.b[CL] & 0x7; - I.ZeroVal = (tmp & (1<>8)&0xf); - tmp &= 0xff; - PutbackRMByte(ModRM,tmp); - CLKM(9,15); - break; - - case 0x2a : - ModRM = FETCH; - tmp = GetRMByte(ModRM); - tmp2 = (I.regs.b[AL] & 0xf)<<4; - I.regs.b[AL] = (I.regs.b[AL] & 0xf0) | (tmp&0xf); - tmp = tmp2 | (tmp>>4); - PutbackRMByte(ModRM,tmp); - CLKM(13,19); - break; - - case 0x31 : - ModRM = FETCH; - ModRM=0; - break; - - case 0x33 : - ModRM = FETCH; - ModRM=0; - break; - - case 0x92 : - CLK(2); - break; /* V25/35 FINT */ - - case 0xe0 : - ModRM = FETCH; - ModRM=0; - break; - - case 0xf0 : - ModRM = FETCH; - ModRM=0; - break; - - case 0xff : - ModRM = FETCH; - ModRM=0; - break; - - default: - break; - } + DEF_wr16; + ADDW; + PutbackRMWord(ModRM, dst); + CLKM(3, 1); } -OP( 0x10, i_adc_br8 ) +OP(0x02, i_add_r8b) { - DEF_br8; - src+=CF; - ADDB; - PutbackRMByte(ModRM,dst); - CLKM(3,1); -} -OP( 0x11, i_adc_wr16 ) -{ - DEF_wr16; - src+=CF; - ADDW; - PutbackRMWord(ModRM,dst); - CLKM(3,1); -} -OP( 0x12, i_adc_r8b ) -{ - DEF_r8b; - src+=CF; - ADDB; - RegByte(ModRM)=dst; - CLKM(2,1); -} -OP( 0x13, i_adc_r16w ) -{ - DEF_r16w; - src+=CF; - ADDW; - RegWord(ModRM)=dst; - CLKM(2,1); -} -OP( 0x14, i_adc_ald8 ) -{ - DEF_ald8; - src+=CF; - ADDB; - I.regs.b[AL]=dst; - CLK(1); -} -OP( 0x15, i_adc_axd16) -{ - DEF_axd16; - src+=CF; - ADDW; - I.regs.w[AW]=dst; - CLK(1); -} -OP( 0x16, i_push_ss ) -{ - PUSH(I.sregs[SS]); - CLK(2); -} -OP( 0x17, i_pop_ss ) -{ - POP(I.sregs[SS]); - CLK(3); - no_interrupt=1; + DEF_r8b; + ADDB; + RegByte(ModRM) = dst; + CLKM(2, 1); } -OP( 0x18, i_sbb_br8 ) +OP(0x03, i_add_r16w) { - DEF_br8; - src+=CF; - SUBB; - PutbackRMByte(ModRM,dst); - CLKM(3,1); -} -OP( 0x19, i_sbb_wr16 ) -{ - DEF_wr16; - src+=CF; - SUBW; - PutbackRMWord(ModRM,dst); - CLKM(3,1); -} -OP( 0x1a, i_sbb_r8b ) -{ - DEF_r8b; - src+=CF; - SUBB; - RegByte(ModRM)=dst; - CLKM(2,1); -} -OP( 0x1b, i_sbb_r16w ) -{ - DEF_r16w; - src+=CF; - SUBW; - RegWord(ModRM)=dst; - CLKM(2,1); -} -OP( 0x1c, i_sbb_ald8 ) -{ - DEF_ald8; - src+=CF; - SUBB; - I.regs.b[AL]=dst; - CLK(1); -} -OP( 0x1d, i_sbb_axd16) -{ - DEF_axd16; - src+=CF; - SUBW; - I.regs.w[AW]=dst; - CLK(1); -} -OP( 0x1e, i_push_ds ) -{ - PUSH(I.sregs[DS]); - CLK(2); -} -OP( 0x1f, i_pop_ds ) -{ - POP(I.sregs[DS]); - CLK(3); + DEF_r16w; + ADDW; + RegWord(ModRM) = dst; + CLKM(2, 1); } -OP( 0x20, i_and_br8 ) +OP(0x04, i_add_ald8) { - DEF_br8; - ANDB; - PutbackRMByte(ModRM,dst); - CLKM(3,1); -} -OP( 0x21, i_and_wr16 ) -{ - DEF_wr16; - ANDW; - PutbackRMWord(ModRM,dst); - CLKM(3,1); -} -OP( 0x22, i_and_r8b ) -{ - DEF_r8b; - ANDB; - RegByte(ModRM)=dst; - CLKM(2,1); -} -OP( 0x23, i_and_r16w ) -{ - DEF_r16w; - ANDW; - RegWord(ModRM)=dst; - CLKM(2,1); -} -OP( 0x24, i_and_ald8 ) -{ - DEF_ald8; - ANDB; - I.regs.b[AL]=dst; - CLK(1); -} -OP( 0x25, i_and_axd16) -{ - DEF_axd16; - ANDW; - I.regs.w[AW]=dst; - CLK(1); -} -OP( 0x26, i_es ) -{ - seg_prefix=TRUE; - prefix_base=I.sregs[ES]<<4; - CLK(1); - nec_instruction[FETCHOP](); - seg_prefix=FALSE; -} -OP( 0x27, i_daa ) -{ - ADJ4(6,0x60); - CLK(10); + DEF_ald8; + ADDB; + I.regs.b[AL] = dst; + CLK(1); } -OP( 0x28, i_sub_br8 ) +OP(0x05, i_add_axd16) { - DEF_br8; - SUBB; - PutbackRMByte(ModRM,dst); - CLKM(3,1); -} -OP( 0x29, i_sub_wr16 ) -{ - DEF_wr16; - SUBW; - PutbackRMWord(ModRM,dst); - CLKM(3,1); -} -OP( 0x2a, i_sub_r8b ) -{ - DEF_r8b; - SUBB; - RegByte(ModRM)=dst; - CLKM(2,1); -} -OP( 0x2b, i_sub_r16w ) -{ - DEF_r16w; - SUBW; - RegWord(ModRM)=dst; - CLKM(2,1); -} -OP( 0x2c, i_sub_ald8 ) -{ - DEF_ald8; - SUBB; - I.regs.b[AL]=dst; - CLK(1); -} -OP( 0x2d, i_sub_axd16) -{ - DEF_axd16; - SUBW; - I.regs.w[AW]=dst; - CLK(1); -} -OP( 0x2e, i_cs ) -{ - seg_prefix=TRUE; - prefix_base=I.sregs[CS]<<4; - CLK(1); - nec_instruction[FETCHOP](); - seg_prefix=FALSE; -} -OP( 0x2f, i_das ) -{ - ADJ4(-6,-0x60); - CLK(10); + DEF_axd16; + ADDW; + I.regs.w[AW] = dst; + CLK(1); } -OP( 0x30, i_xor_br8 ) +OP(0x06, i_push_es) { - DEF_br8; - XORB; - PutbackRMByte(ModRM,dst); - CLKM(3,1); -} -OP( 0x31, i_xor_wr16 ) -{ - DEF_wr16; - XORW; - PutbackRMWord(ModRM,dst); - CLKM(3,1); -} -OP( 0x32, i_xor_r8b ) -{ - DEF_r8b; - XORB; - RegByte(ModRM)=dst; - CLKM(2,1); -} -OP( 0x33, i_xor_r16w ) -{ - DEF_r16w; - XORW; - RegWord(ModRM)=dst; - CLKM(2,1); -} -OP( 0x34, i_xor_ald8 ) -{ - DEF_ald8; - XORB; - I.regs.b[AL]=dst; - CLK(1); -} -OP( 0x35, i_xor_axd16) -{ - DEF_axd16; - XORW; - I.regs.w[AW]=dst; - CLK(1); -} -OP( 0x36, i_ss ) -{ - seg_prefix=TRUE; - prefix_base=I.sregs[SS]<<4; - CLK(1); - nec_instruction[FETCHOP](); - seg_prefix=FALSE; -} -OP( 0x37, i_aaa ) -{ - ADJB(6,1); - CLK(9); + PUSH(I.sregs[ES]); + CLK(2); } -OP( 0x38, i_cmp_br8 ) +OP(0x07, i_pop_es) { - DEF_br8; - SUBB; - CLKM(2,1); -} -OP( 0x39, i_cmp_wr16 ) -{ - DEF_wr16; - SUBW; - CLKM(2,1); -} -OP( 0x3a, i_cmp_r8b ) -{ - DEF_r8b; - SUBB; - CLKM(2,1); -} -OP( 0x3b, i_cmp_r16w ) -{ - DEF_r16w; - SUBW; - CLKM(2,1); -} -OP( 0x3c, i_cmp_ald8 ) -{ - DEF_ald8; - SUBB; - CLK(1); -} -OP( 0x3d, i_cmp_axd16) -{ - DEF_axd16; - SUBW; - CLK(1); -} -OP( 0x3e, i_ds ) -{ - seg_prefix=TRUE; - prefix_base=I.sregs[DS]<<4; - CLK(1); - nec_instruction[FETCHOP](); - seg_prefix=FALSE; -} -OP( 0x3f, i_aas ) -{ - ADJB(-6,-1); - CLK(9); + POP(I.sregs[ES]); + CLK(3); } -OP( 0x40, i_inc_ax ) +OP(0x08, i_or_br8) { - IncWordReg(AW); - CLK(1); -} -OP( 0x41, i_inc_cx ) -{ - IncWordReg(CW); - CLK(1); -} -OP( 0x42, i_inc_dx ) -{ - IncWordReg(DW); - CLK(1); -} -OP( 0x43, i_inc_bx ) -{ - IncWordReg(BW); - CLK(1); -} -OP( 0x44, i_inc_sp ) -{ - IncWordReg(SP); - CLK(1); -} -OP( 0x45, i_inc_bp ) -{ - IncWordReg(BP); - CLK(1); -} -OP( 0x46, i_inc_si ) -{ - IncWordReg(IX); - CLK(1); -} -OP( 0x47, i_inc_di ) -{ - IncWordReg(IY); - CLK(1); + DEF_br8; + ORB; + PutbackRMByte(ModRM, dst); + CLKM(3, 1); } -OP( 0x48, i_dec_ax ) +OP(0x09, i_or_wr16) { - DecWordReg(AW); - CLK(1); -} -OP( 0x49, i_dec_cx ) -{ - DecWordReg(CW); - CLK(1); -} -OP( 0x4a, i_dec_dx ) -{ - DecWordReg(DW); - CLK(1); -} -OP( 0x4b, i_dec_bx ) -{ - DecWordReg(BW); - CLK(1); -} -OP( 0x4c, i_dec_sp ) -{ - DecWordReg(SP); - CLK(1); -} -OP( 0x4d, i_dec_bp ) -{ - DecWordReg(BP); - CLK(1); -} -OP( 0x4e, i_dec_si ) -{ - DecWordReg(IX); - CLK(1); -} -OP( 0x4f, i_dec_di ) -{ - DecWordReg(IY); - CLK(1); + DEF_wr16; + ORW; + PutbackRMWord(ModRM, dst); + CLKM(3, 1); } -OP( 0x50, i_push_ax ) +OP(0x0a, i_or_r8b) { - PUSH(I.regs.w[AW]); - CLK(1); -} -OP( 0x51, i_push_cx ) -{ - PUSH(I.regs.w[CW]); - CLK(1); -} -OP( 0x52, i_push_dx ) -{ - PUSH(I.regs.w[DW]); - CLK(1); -} -OP( 0x53, i_push_bx ) -{ - PUSH(I.regs.w[BW]); - CLK(1); -} -OP( 0x54, i_push_sp ) -{ - PUSH(I.regs.w[SP]); - CLK(1); -} -OP( 0x55, i_push_bp ) -{ - PUSH(I.regs.w[BP]); - CLK(1); -} -OP( 0x56, i_push_si ) -{ - PUSH(I.regs.w[IX]); - CLK(1); -} -OP( 0x57, i_push_di ) -{ - PUSH(I.regs.w[IY]); - CLK(1); + DEF_r8b; + ORB; + RegByte(ModRM) = dst; + CLKM(2, 1); } -OP( 0x58, i_pop_ax ) +OP(0x0b, i_or_r16w) { - POP(I.regs.w[AW]); - CLK(1); -} -OP( 0x59, i_pop_cx ) -{ - POP(I.regs.w[CW]); - CLK(1); -} -OP( 0x5a, i_pop_dx ) -{ - POP(I.regs.w[DW]); - CLK(1); -} -OP( 0x5b, i_pop_bx ) -{ - POP(I.regs.w[BW]); - CLK(1); -} -OP( 0x5c, i_pop_sp ) -{ - POP(I.regs.w[SP]); - CLK(1); -} -OP( 0x5d, i_pop_bp ) -{ - POP(I.regs.w[BP]); - CLK(1); -} -OP( 0x5e, i_pop_si ) -{ - POP(I.regs.w[IX]); - CLK(1); -} -OP( 0x5f, i_pop_di ) -{ - POP(I.regs.w[IY]); - CLK(1); + DEF_r16w; + ORW; + RegWord(ModRM) = dst; + CLKM(2, 1); } -OP( 0x60, i_pusha ) +OP(0x0c, i_or_ald8) { - uint32_t tmp=I.regs.w[SP]; - PUSH(I.regs.w[AW]); - PUSH(I.regs.w[CW]); - PUSH(I.regs.w[DW]); - PUSH(I.regs.w[BW]); - PUSH(tmp); - PUSH(I.regs.w[BP]); - PUSH(I.regs.w[IX]); - PUSH(I.regs.w[IY]); - CLK(9); + DEF_ald8; + ORB; + I.regs.b[AL] = dst; + CLK(1); } -OP( 0x61, i_popa ) + +OP(0x0d, i_or_axd16) { - uint32_t tmp; - POP(I.regs.w[IY]); - POP(I.regs.w[IX]); - POP(I.regs.w[BP]); - POP(tmp); - POP(I.regs.w[BW]); - POP(I.regs.w[DW]); - POP(I.regs.w[CW]); - POP(I.regs.w[AW]); - (void)tmp; // We need to uppop something and need tmp - CLK(8); + DEF_axd16; + ORW; + I.regs.w[AW] = dst; + CLK(1); +} + +OP(0x0e, i_push_cs) +{ + PUSH(I.sregs[CS]); + CLK(2); +} + +OP(0x0f, i_pre_nec) +{ + uint32_t ModRM, tmp, tmp2; /* pop cs at V30MZ? */ + + switch (FETCH) + { + case 0x10 : + BITOP_BYTE; + CLKS(3, 3, 4); + tmp2 = I.regs.b[CL] & 0x7; + I.ZeroVal = (tmp & (1 << tmp2)) ? 1 : 0; + I.CarryVal = I.OverVal = 0; + break; /* Test */ + + case 0x11 : + BITOP_WORD; + CLKS(3, 3, 4); + tmp2 = I.regs.b[CL] & 0xf; + I.ZeroVal = (tmp & (1 << tmp2)) ? 1 : 0; + I.CarryVal = I.OverVal = 0; + break; /* Test */ + + case 0x12 : + BITOP_BYTE; + CLKS(5, 5, 4); + tmp2 = I.regs.b[CL] & 0x7; + tmp &= ~(1 << tmp2); + PutbackRMByte(ModRM, tmp); + break; /* Clr */ + + case 0x13 : + BITOP_WORD; + CLKS(5, 5, 4); + tmp2 = I.regs.b[CL] & 0xf; + tmp &= ~(1 << tmp2); + PutbackRMWord(ModRM, tmp); + break; /* Clr */ + + case 0x14 : + BITOP_BYTE; + CLKS(4, 4, 4); + tmp2 = I.regs.b[CL] & 0x7; + tmp |= (1 << tmp2); + PutbackRMByte(ModRM, tmp); + break; /* Set */ + + case 0x15 : + BITOP_WORD; + CLKS(4, 4, 4); + tmp2 = I.regs.b[CL] & 0xf; + tmp |= (1 << tmp2); + PutbackRMWord(ModRM, tmp); + break; /* Set */ + + case 0x16 : + BITOP_BYTE; + CLKS(4, 4, 4); + tmp2 = I.regs.b[CL] & 0x7; + BIT_NOT; + PutbackRMByte(ModRM, tmp); + break; /* Not */ + + case 0x17 : + BITOP_WORD; + CLKS(4, 4, 4); + tmp2 = I.regs.b[CL] & 0xf; + BIT_NOT; + PutbackRMWord(ModRM, tmp); + break; /* Not */ + + case 0x18 : + BITOP_BYTE; + CLKS(4, 4, 4); + tmp2 = (FETCH) & 0x7; + I.ZeroVal = (tmp & (1 << tmp2)) ? 1 : 0; + I.CarryVal = I.OverVal = 0; + break; /* Test */ + + case 0x19 : + BITOP_WORD; + CLKS(4, 4, 4); + tmp2 = (FETCH) & 0xf; + I.ZeroVal = (tmp & (1 << tmp2)) ? 1 : 0; + I.CarryVal = I.OverVal = 0; + break; /* Test */ + + case 0x1a : + BITOP_BYTE; + CLKS(6, 6, 4); + tmp2 = (FETCH) & 0x7; + tmp &= ~(1 << tmp2); + PutbackRMByte(ModRM, tmp); + break; /* Clr */ + + case 0x1b : + BITOP_WORD; + CLKS(6, 6, 4); + tmp2 = (FETCH) & 0xf; + tmp &= ~(1 << tmp2); + PutbackRMWord(ModRM, tmp); + break; /* Clr */ + + case 0x1c : + BITOP_BYTE; + CLKS(5, 5, 4); + tmp2 = (FETCH) & 0x7; + tmp |= (1 << tmp2); + PutbackRMByte(ModRM, tmp); + break; /* Set */ + + case 0x1d : + BITOP_WORD; + CLKS(5, 5, 4); + tmp2 = (FETCH) & 0xf; + tmp |= (1 << tmp2); + PutbackRMWord(ModRM, tmp); + break; /* Set */ + + case 0x1e : + BITOP_BYTE; + CLKS(5, 5, 4); + tmp2 = (FETCH) & 0x7; + BIT_NOT; + PutbackRMByte(ModRM, tmp); + break; /* Not */ + + case 0x1f : + BITOP_WORD; + CLKS(5, 5, 4); + tmp2 = (FETCH) & 0xf; + BIT_NOT; + PutbackRMWord(ModRM, tmp); + break; /* Not */ + + case 0x20 : + ADD4S; + CLKS(7, 7, 2); + break; + + case 0x22 : + SUB4S; + CLKS(7, 7, 2); + break; + + case 0x26 : + CMP4S; + CLKS(7, 7, 2); + break; + + case 0x28 : + ModRM = FETCH; + tmp = GetRMByte(ModRM); + tmp <<= 4; + tmp |= I.regs.b[AL] & 0xf; + I.regs.b[AL] = (I.regs.b[AL] & 0xf0) | ((tmp >> 8) & 0xf); + tmp &= 0xff; + PutbackRMByte(ModRM, tmp); + CLKM(9, 15); + break; + + case 0x2a : + ModRM = FETCH; + tmp = GetRMByte(ModRM); + tmp2 = (I.regs.b[AL] & 0xf) << 4; + I.regs.b[AL] = (I.regs.b[AL] & 0xf0) | (tmp & 0xf); + tmp = tmp2 | (tmp >> 4); + PutbackRMByte(ModRM, tmp); + CLKM(13, 19); + break; + + case 0x31 : + ModRM = FETCH; + ModRM = 0; + break; + + case 0x33 : + ModRM = FETCH; + ModRM = 0; + break; + + case 0x92 : + CLK(2); + break; /* V25/35 FINT */ + + case 0xe0 : + ModRM = FETCH; + ModRM = 0; + break; + + case 0xf0 : + ModRM = FETCH; + ModRM = 0; + break; + + case 0xff : + ModRM = FETCH; + ModRM = 0; + break; + + default: + break; + } +} + +OP(0x10, i_adc_br8) +{ + DEF_br8; + src += CF; + ADDB; + PutbackRMByte(ModRM, dst); + CLKM(3, 1); +} + +OP(0x11, i_adc_wr16) +{ + DEF_wr16; + src += CF; + ADDW; + PutbackRMWord(ModRM, dst); + CLKM(3, 1); +} + +OP(0x12, i_adc_r8b) +{ + DEF_r8b; + src += CF; + ADDB; + RegByte(ModRM) = dst; + CLKM(2, 1); +} + +OP(0x13, i_adc_r16w) +{ + DEF_r16w; + src += CF; + ADDW; + RegWord(ModRM) = dst; + CLKM(2, 1); +} + +OP(0x14, i_adc_ald8) +{ + DEF_ald8; + src += CF; + ADDB; + I.regs.b[AL] = dst; + CLK(1); +} + +OP(0x15, i_adc_axd16) +{ + DEF_axd16; + src += CF; + ADDW; + I.regs.w[AW] = dst; + CLK(1); +} + +OP(0x16, i_push_ss) +{ + PUSH(I.sregs[SS]); + CLK(2); +} + +OP(0x17, i_pop_ss) +{ + POP(I.sregs[SS]); + CLK(3); + no_interrupt = 1; +} + +OP(0x18, i_sbb_br8) +{ + DEF_br8; + src += CF; + SUBB; + PutbackRMByte(ModRM, dst); + CLKM(3, 1); +} + +OP(0x19, i_sbb_wr16) +{ + DEF_wr16; + src += CF; + SUBW; + PutbackRMWord(ModRM, dst); + CLKM(3, 1); +} + +OP(0x1a, i_sbb_r8b) +{ + DEF_r8b; + src += CF; + SUBB; + RegByte(ModRM) = dst; + CLKM(2, 1); +} + +OP(0x1b, i_sbb_r16w) +{ + DEF_r16w; + src += CF; + SUBW; + RegWord(ModRM) = dst; + CLKM(2, 1); +} + +OP(0x1c, i_sbb_ald8) +{ + DEF_ald8; + src += CF; + SUBB; + I.regs.b[AL] = dst; + CLK(1); +} + +OP(0x1d, i_sbb_axd16) +{ + DEF_axd16; + src += CF; + SUBW; + I.regs.w[AW] = dst; + CLK(1); +} + +OP(0x1e, i_push_ds) +{ + PUSH(I.sregs[DS]); + CLK(2); +} + +OP(0x1f, i_pop_ds) +{ + POP(I.sregs[DS]); + CLK(3); +} + +OP(0x20, i_and_br8) +{ + DEF_br8; + ANDB; + PutbackRMByte(ModRM, dst); + CLKM(3, 1); +} + +OP(0x21, i_and_wr16) +{ + DEF_wr16; + ANDW; + PutbackRMWord(ModRM, dst); + CLKM(3, 1); +} + +OP(0x22, i_and_r8b) +{ + DEF_r8b; + ANDB; + RegByte(ModRM) = dst; + CLKM(2, 1); +} + +OP(0x23, i_and_r16w) +{ + DEF_r16w; + ANDW; + RegWord(ModRM) = dst; + CLKM(2, 1); +} + +OP(0x24, i_and_ald8) +{ + DEF_ald8; + ANDB; + I.regs.b[AL] = dst; + CLK(1); +} + +OP(0x25, i_and_axd16) +{ + DEF_axd16; + ANDW; + I.regs.w[AW] = dst; + CLK(1); +} + +OP(0x26, i_es) +{ + seg_prefix = TRUE; + prefix_base = I.sregs[ES] << 4; + CLK(1); + nec_instruction[FETCHOP](); + seg_prefix = FALSE; +} + +OP(0x27, i_daa) +{ + ADJ4(6, 0x60); + CLK(10); +} + +OP(0x28, i_sub_br8) +{ + DEF_br8; + SUBB; + PutbackRMByte(ModRM, dst); + CLKM(3, 1); +} + +OP(0x29, i_sub_wr16) +{ + DEF_wr16; + SUBW; + PutbackRMWord(ModRM, dst); + CLKM(3, 1); +} + +OP(0x2a, i_sub_r8b) +{ + DEF_r8b; + SUBB; + RegByte(ModRM) = dst; + CLKM(2, 1); +} + +OP(0x2b, i_sub_r16w) +{ + DEF_r16w; + SUBW; + RegWord(ModRM) = dst; + CLKM(2, 1); +} + +OP(0x2c, i_sub_ald8) +{ + DEF_ald8; + SUBB; + I.regs.b[AL] = dst; + CLK(1); +} + +OP(0x2d, i_sub_axd16) +{ + DEF_axd16; + SUBW; + I.regs.w[AW] = dst; + CLK(1); +} + +OP(0x2e, i_cs) +{ + seg_prefix = TRUE; + prefix_base = I.sregs[CS] << 4; + CLK(1); + nec_instruction[FETCHOP](); + seg_prefix = FALSE; +} + +OP(0x2f, i_das) +{ + ADJ4(-6, -0x60); + CLK(10); +} + +OP(0x30, i_xor_br8) +{ + DEF_br8; + XORB; + PutbackRMByte(ModRM, dst); + CLKM(3, 1); +} + +OP(0x31, i_xor_wr16) +{ + DEF_wr16; + XORW; + PutbackRMWord(ModRM, dst); + CLKM(3, 1); +} + +OP(0x32, i_xor_r8b) +{ + DEF_r8b; + XORB; + RegByte(ModRM) = dst; + CLKM(2, 1); +} + +OP(0x33, i_xor_r16w) +{ + DEF_r16w; + XORW; + RegWord(ModRM) = dst; + CLKM(2, 1); +} + +OP(0x34, i_xor_ald8) +{ + DEF_ald8; + XORB; + I.regs.b[AL] = dst; + CLK(1); +} + +OP(0x35, i_xor_axd16) +{ + DEF_axd16; + XORW; + I.regs.w[AW] = dst; + CLK(1); +} + +OP(0x36, i_ss) +{ + seg_prefix = TRUE; + prefix_base = I.sregs[SS] << 4; + CLK(1); + nec_instruction[FETCHOP](); + seg_prefix = FALSE; +} + +OP(0x37, i_aaa) +{ + ADJB(6, 1); + CLK(9); +} + +OP(0x38, i_cmp_br8) +{ + DEF_br8; + SUBB; + CLKM(2, 1); +} + +OP(0x39, i_cmp_wr16) +{ + DEF_wr16; + SUBW; + CLKM(2, 1); +} + +OP(0x3a, i_cmp_r8b) +{ + DEF_r8b; + SUBB; + CLKM(2, 1); +} + +OP(0x3b, i_cmp_r16w) +{ + DEF_r16w; + SUBW; + CLKM(2, 1); +} + +OP(0x3c, i_cmp_ald8) +{ + DEF_ald8; + SUBB; + CLK(1); +} + +OP(0x3d, i_cmp_axd16) +{ + DEF_axd16; + SUBW; + CLK(1); +} + +OP(0x3e, i_ds) +{ + seg_prefix = TRUE; + prefix_base = I.sregs[DS] << 4; + CLK(1); + nec_instruction[FETCHOP](); + seg_prefix = FALSE; +} + +OP(0x3f, i_aas) +{ + ADJB(-6, -1); + CLK(9); +} + +OP(0x40, i_inc_ax) +{ + IncWordReg(AW); + CLK(1); +} + +OP(0x41, i_inc_cx) +{ + IncWordReg(CW); + CLK(1); +} + +OP(0x42, i_inc_dx) +{ + IncWordReg(DW); + CLK(1); +} + +OP(0x43, i_inc_bx) +{ + IncWordReg(BW); + CLK(1); +} + +OP(0x44, i_inc_sp) +{ + IncWordReg(SP); + CLK(1); +} + +OP(0x45, i_inc_bp) +{ + IncWordReg(BP); + CLK(1); +} + +OP(0x46, i_inc_si) +{ + IncWordReg(IX); + CLK(1); +} + +OP(0x47, i_inc_di) +{ + IncWordReg(IY); + CLK(1); +} + +OP(0x48, i_dec_ax) +{ + DecWordReg(AW); + CLK(1); +} + +OP(0x49, i_dec_cx) +{ + DecWordReg(CW); + CLK(1); +} + +OP(0x4a, i_dec_dx) +{ + DecWordReg(DW); + CLK(1); +} + +OP(0x4b, i_dec_bx) +{ + DecWordReg(BW); + CLK(1); +} + +OP(0x4c, i_dec_sp) +{ + DecWordReg(SP); + CLK(1); +} + +OP(0x4d, i_dec_bp) +{ + DecWordReg(BP); + CLK(1); +} + +OP(0x4e, i_dec_si) +{ + DecWordReg(IX); + CLK(1); +} + +OP(0x4f, i_dec_di) +{ + DecWordReg(IY); + CLK(1); +} + +OP(0x50, i_push_ax) +{ + PUSH(I.regs.w[AW]); + CLK(1); +} + +OP(0x51, i_push_cx) +{ + PUSH(I.regs.w[CW]); + CLK(1); +} + +OP(0x52, i_push_dx) +{ + PUSH(I.regs.w[DW]); + CLK(1); +} + +OP(0x53, i_push_bx) +{ + PUSH(I.regs.w[BW]); + CLK(1); +} + +OP(0x54, i_push_sp) +{ + PUSH(I.regs.w[SP]); + CLK(1); +} + +OP(0x55, i_push_bp) +{ + PUSH(I.regs.w[BP]); + CLK(1); +} + +OP(0x56, i_push_si) +{ + PUSH(I.regs.w[IX]); + CLK(1); +} + +OP(0x57, i_push_di) +{ + PUSH(I.regs.w[IY]); + CLK(1); +} + +OP(0x58, i_pop_ax) +{ + POP(I.regs.w[AW]); + CLK(1); +} + +OP(0x59, i_pop_cx) +{ + POP(I.regs.w[CW]); + CLK(1); +} + +OP(0x5a, i_pop_dx) +{ + POP(I.regs.w[DW]); + CLK(1); +} + +OP(0x5b, i_pop_bx) +{ + POP(I.regs.w[BW]); + CLK(1); +} + +OP(0x5c, i_pop_sp) +{ + POP(I.regs.w[SP]); + CLK(1); +} + +OP(0x5d, i_pop_bp) +{ + POP(I.regs.w[BP]); + CLK(1); +} + +OP(0x5e, i_pop_si) +{ + POP(I.regs.w[IX]); + CLK(1); +} + +OP(0x5f, i_pop_di) +{ + POP(I.regs.w[IY]); + CLK(1); +} + +OP(0x60, i_pusha) +{ + uint32_t tmp = I.regs.w[SP]; + PUSH(I.regs.w[AW]); + PUSH(I.regs.w[CW]); + PUSH(I.regs.w[DW]); + PUSH(I.regs.w[BW]); + PUSH(tmp); + PUSH(I.regs.w[BP]); + PUSH(I.regs.w[IX]); + PUSH(I.regs.w[IY]); + CLK(9); +} + +OP(0x61, i_popa) +{ + uint32_t tmp; + POP(I.regs.w[IY]); + POP(I.regs.w[IX]); + POP(I.regs.w[BP]); + POP(tmp); + POP(I.regs.w[BW]); + POP(I.regs.w[DW]); + POP(I.regs.w[CW]); + POP(I.regs.w[AW]); + (void)tmp; // We need to uppop something and need tmp + CLK(8); } /* BOUND */ -OP( 0x62, i_chkind ) +OP(0x62, i_chkind) { - uint32_t low,high,tmp; - GetModRM; - low = GetRMWord(ModRM); - high= GetnextRMWord; - tmp= RegWord(ModRM); + uint32_t low, high, tmp; + GetModRM; + low = GetRMWord(ModRM); + high = GetnextRMWord; + tmp = RegWord(ModRM); - if (tmphigh) - { - nec_interrupt(5,0); - CLK(7); - } + if (tmp < low || tmp > high) + { + nec_interrupt(5, 0); + CLK(7); + } - CLK(13); + CLK(13); } /* OP 0x64 - 0x67 is nop at V30MZ */ -OP( 0x64, i_repnc ) -{ - uint32_t next = FETCHOP; - uint16_t c = I.regs.w[CW]; - - switch(next) /* Segments */ - { - case 0x26: - seg_prefix=TRUE; - prefix_base=I.sregs[ES]<<4; - next = FETCHOP; - CLK(2); - break; - - case 0x2e: - seg_prefix=TRUE; - prefix_base=I.sregs[CS]<<4; - next = FETCHOP; - CLK(2); - break; - - case 0x36: - seg_prefix=TRUE; - prefix_base=I.sregs[SS]<<4; - next = FETCHOP; - CLK(2); - break; - - case 0x3e: - seg_prefix=TRUE; - prefix_base=I.sregs[DS]<<4; - next = FETCHOP; - CLK(2); - break; - } - - switch(next) - { - case 0x6c: - CLK(2); - - if (c) do - { - i_insb(); - c--; - } - while (c>0 && !CF); - - I.regs.w[CW]=c; - break; - - case 0x6d: - CLK(2); - - if (c) do - { - i_insw(); - c--; - } - while (c>0 && !CF); - - I.regs.w[CW]=c; - break; - - case 0x6e: - CLK(2); - - if (c) do - { - i_outsb(); - c--; - } - while (c>0 && !CF); - - I.regs.w[CW]=c; - break; - - case 0x6f: - CLK(2); - - if (c) do - { - i_outsw(); - c--; - } - while (c>0 && !CF); - - I.regs.w[CW]=c; - break; - - case 0xa4: - CLK(2); - - if (c) do - { - i_movsb(); - c--; - } - while (c>0 && !CF); - - I.regs.w[CW]=c; - break; - - case 0xa5: - CLK(2); - - if (c) do - { - i_movsw(); - c--; - } - while (c>0 && !CF); - - I.regs.w[CW]=c; - break; - - case 0xa6: - CLK(2); - - if (c) do - { - i_cmpsb(); - c--; - } - while (c>0 && !CF); - - I.regs.w[CW]=c; - break; - - case 0xa7: - CLK(2); - - if (c) do - { - i_cmpsw(); - c--; - } - while (c>0 && !CF); - - I.regs.w[CW]=c; - break; - - case 0xaa: - CLK(2); - - if (c) do - { - i_stosb(); - c--; - } - while (c>0 && !CF); - - I.regs.w[CW]=c; - break; - - case 0xab: - CLK(2); - - if (c) do - { - i_stosw(); - c--; - } - while (c>0 && !CF); - - I.regs.w[CW]=c; - break; - - case 0xac: - CLK(2); - - if (c) do - { - i_lodsb(); - c--; - } - while (c>0 && !CF); - - I.regs.w[CW]=c; - break; - - case 0xad: - CLK(2); - - if (c) do - { - i_lodsw(); - c--; - } - while (c>0 && !CF); - - I.regs.w[CW]=c; - break; - - case 0xae: - CLK(2); - - if (c) do - { - i_scasb(); - c--; - } - while (c>0 && !CF); - - I.regs.w[CW]=c; - break; - - case 0xaf: - CLK(2); - - if (c) do - { - i_scasw(); - c--; - } - while (c>0 && !CF); - - I.regs.w[CW]=c; - break; - - default: - nec_instruction[next](); - } - - seg_prefix=FALSE; -} - -OP( 0x65, i_repc ) -{ - uint32_t next = FETCHOP; - uint16_t c = I.regs.w[CW]; - - switch(next) /* Segments */ - { - case 0x26: - seg_prefix=TRUE; - prefix_base=I.sregs[ES]<<4; - next = FETCHOP; - CLK(2); - break; - - case 0x2e: - seg_prefix=TRUE; - prefix_base=I.sregs[CS]<<4; - next = FETCHOP; - CLK(2); - break; - - case 0x36: - seg_prefix=TRUE; - prefix_base=I.sregs[SS]<<4; - next = FETCHOP; - CLK(2); - break; - - case 0x3e: - seg_prefix=TRUE; - prefix_base=I.sregs[DS]<<4; - next = FETCHOP; - CLK(2); - break; - } - - switch(next) - { - case 0x6c: - CLK(2); - - if (c) do - { - i_insb(); - c--; - } - while (c>0 && CF); - - I.regs.w[CW]=c; - break; - - case 0x6d: - CLK(2); - - if (c) do - { - i_insw(); - c--; - } - while (c>0 && CF); - - I.regs.w[CW]=c; - break; - - case 0x6e: - CLK(2); - - if (c) do - { - i_outsb(); - c--; - } - while (c>0 && CF); - - I.regs.w[CW]=c; - break; - - case 0x6f: - CLK(2); - - if (c) do - { - i_outsw(); - c--; - } - while (c>0 && CF); - - I.regs.w[CW]=c; - break; - - case 0xa4: - CLK(2); - - if (c) do - { - i_movsb(); - c--; - } - while (c>0 && CF); - - I.regs.w[CW]=c; - break; - - case 0xa5: - CLK(2); - - if (c) do - { - i_movsw(); - c--; - } - while (c>0 && CF); - - I.regs.w[CW]=c; - break; - - case 0xa6: - CLK(2); - - if (c) do - { - i_cmpsb(); - c--; - } - while (c>0 && CF); - - I.regs.w[CW]=c; - break; - - case 0xa7: - CLK(2); - - if (c) do - { - i_cmpsw(); - c--; - } - while (c>0 && CF); - - I.regs.w[CW]=c; - break; - - case 0xaa: - CLK(2); - - if (c) do - { - i_stosb(); - c--; - } - while (c>0 && CF); - - I.regs.w[CW]=c; - break; - - case 0xab: - CLK(2); - - if (c) do - { - i_stosw(); - c--; - } - while (c>0 && CF); - - I.regs.w[CW]=c; - break; - - case 0xac: - CLK(2); - - if (c) do - { - i_lodsb(); - c--; - } - while (c>0 && CF); - - I.regs.w[CW]=c; - break; +OP(0x64, i_repnc) +{ + uint32_t next = FETCHOP; + uint16_t c = I.regs.w[CW]; + + switch (next) /* Segments */ + { + case 0x26: + seg_prefix = TRUE; + prefix_base = I.sregs[ES] << 4; + next = FETCHOP; + CLK(2); + break; + + case 0x2e: + seg_prefix = TRUE; + prefix_base = I.sregs[CS] << 4; + next = FETCHOP; + CLK(2); + break; + + case 0x36: + seg_prefix = TRUE; + prefix_base = I.sregs[SS] << 4; + next = FETCHOP; + CLK(2); + break; + + case 0x3e: + seg_prefix = TRUE; + prefix_base = I.sregs[DS] << 4; + next = FETCHOP; + CLK(2); + break; + } + + switch (next) + { + case 0x6c: + CLK(2); + + if (c) + { + do + { + i_insb(); + c--; + } while (c > 0 && !CF); + } + + I.regs.w[CW] = c; + break; + + case 0x6d: + CLK(2); + + if (c) + { + do + { + i_insw(); + c--; + } while (c > 0 && !CF); + } + + I.regs.w[CW] = c; + break; + + case 0x6e: + CLK(2); + + if (c) + { + do + { + i_outsb(); + c--; + } while (c > 0 && !CF); + } + + I.regs.w[CW] = c; + break; + + case 0x6f: + CLK(2); + + if (c) + { + do + { + i_outsw(); + c--; + } while (c > 0 && !CF); + } + + I.regs.w[CW] = c; + break; + + case 0xa4: + CLK(2); + + if (c) + { + do + { + i_movsb(); + c--; + } while (c > 0 && !CF); + } + + I.regs.w[CW] = c; + break; + + case 0xa5: + CLK(2); + + if (c) + { + do + { + i_movsw(); + c--; + } while (c > 0 && !CF); + } + + I.regs.w[CW] = c; + break; + + case 0xa6: + CLK(2); + + if (c) + { + do + { + i_cmpsb(); + c--; + } while (c > 0 && !CF); + } + + I.regs.w[CW] = c; + break; + + case 0xa7: + CLK(2); + + if (c) + { + do + { + i_cmpsw(); + c--; + } while (c > 0 && !CF); + } + + I.regs.w[CW] = c; + break; + + case 0xaa: + CLK(2); + + if (c) + { + do + { + i_stosb(); + c--; + } while (c > 0 && !CF); + } + + I.regs.w[CW] = c; + break; + + case 0xab: + CLK(2); + + if (c) + { + do + { + i_stosw(); + c--; + } while (c > 0 && !CF); + } + + I.regs.w[CW] = c; + break; + + case 0xac: + CLK(2); + + if (c) + { + do + { + i_lodsb(); + c--; + } while (c > 0 && !CF); + } + + I.regs.w[CW] = c; + break; + + case 0xad: + CLK(2); + + if (c) + { + do + { + i_lodsw(); + c--; + } while (c > 0 && !CF); + } + + I.regs.w[CW] = c; + break; + + case 0xae: + CLK(2); + + if (c) + { + do + { + i_scasb(); + c--; + } while (c > 0 && !CF); + } + + I.regs.w[CW] = c; + break; + + case 0xaf: + CLK(2); + + if (c) + { + do + { + i_scasw(); + c--; + } while (c > 0 && !CF); + } + + I.regs.w[CW] = c; + break; + + default: + nec_instruction[next](); + } + + seg_prefix = FALSE; +} + +OP(0x65, i_repc) +{ + uint32_t next = FETCHOP; + uint16_t c = I.regs.w[CW]; + + switch (next) /* Segments */ + { + case 0x26: + seg_prefix = TRUE; + prefix_base = I.sregs[ES] << 4; + next = FETCHOP; + CLK(2); + break; + + case 0x2e: + seg_prefix = TRUE; + prefix_base = I.sregs[CS] << 4; + next = FETCHOP; + CLK(2); + break; + + case 0x36: + seg_prefix = TRUE; + prefix_base = I.sregs[SS] << 4; + next = FETCHOP; + CLK(2); + break; + + case 0x3e: + seg_prefix = TRUE; + prefix_base = I.sregs[DS] << 4; + next = FETCHOP; + CLK(2); + break; + } + + switch (next) + { + case 0x6c: + CLK(2); + + if (c) + { + do + { + i_insb(); + c--; + } while (c > 0 && CF); + } + + I.regs.w[CW] = c; + break; + + case 0x6d: + CLK(2); + + if (c) + { + do + { + i_insw(); + c--; + } while (c > 0 && CF); + } + + I.regs.w[CW] = c; + break; + + case 0x6e: + CLK(2); + + if (c) + { + do + { + i_outsb(); + c--; + } while (c > 0 && CF); + } + + I.regs.w[CW] = c; + break; + + case 0x6f: + CLK(2); + + if (c) + { + do + { + i_outsw(); + c--; + } while (c > 0 && CF); + } + + I.regs.w[CW] = c; + break; + + case 0xa4: + CLK(2); + + if (c) + { + do + { + i_movsb(); + c--; + } while (c > 0 && CF); + } + + I.regs.w[CW] = c; + break; + + case 0xa5: + CLK(2); + + if (c) + { + do + { + i_movsw(); + c--; + } while (c > 0 && CF); + } + + I.regs.w[CW] = c; + break; + + case 0xa6: + CLK(2); + + if (c) + { + do + { + i_cmpsb(); + c--; + } while (c > 0 && CF); + } + + I.regs.w[CW] = c; + break; + + case 0xa7: + CLK(2); + + if (c) + { + do + { + i_cmpsw(); + c--; + } while (c > 0 && CF); + } + + I.regs.w[CW] = c; + break; + + case 0xaa: + CLK(2); + + if (c) + { + do + { + i_stosb(); + c--; + } while (c > 0 && CF); + } + + I.regs.w[CW] = c; + break; + + case 0xab: + CLK(2); + + if (c) + { + do + { + i_stosw(); + c--; + } while (c > 0 && CF); + } + + I.regs.w[CW] = c; + break; + + case 0xac: + CLK(2); + + if (c) + { + do + { + i_lodsb(); + c--; + } while (c > 0 && CF); + } + + I.regs.w[CW] = c; + break; + + case 0xad: + CLK(2); + + if (c) + { + do + { + i_lodsw(); + c--; + } while (c > 0 && CF); + } - case 0xad: - CLK(2); + I.regs.w[CW] = c; + break; - if (c) do - { - i_lodsw(); - c--; - } - while (c>0 && CF); + case 0xae: + CLK(2); - I.regs.w[CW]=c; - break; + if (c) + { + do + { + i_scasb(); + c--; + } while (c > 0 && CF); + } - case 0xae: - CLK(2); + I.regs.w[CW] = c; + break; - if (c) do - { - i_scasb(); - c--; - } - while (c>0 && CF); + case 0xaf: + CLK(2); - I.regs.w[CW]=c; - break; + if (c) + { + do + { + i_scasw(); + c--; + } while (c > 0 && CF); + } - case 0xaf: - CLK(2); + I.regs.w[CW] = c; + break; - if (c) do - { - i_scasw(); - c--; - } - while (c>0 && CF); + default: + nec_instruction[next](); + } - I.regs.w[CW]=c; - break; - - default: - nec_instruction[next](); - } - - seg_prefix=FALSE; + seg_prefix = FALSE; } -OP( 0x68, i_push_d16 ) +OP(0x68, i_push_d16) { - uint32_t tmp; - FETCHWORD(tmp); - PUSH(tmp); - CLK(1); + uint32_t tmp; + FETCHWORD(tmp); + PUSH(tmp); + CLK(1); } -OP( 0x69, i_imul_d16 ) + +OP(0x69, i_imul_d16) { - uint32_t tmp; - DEF_r16w; - FETCHWORD(tmp); - dst = (int32_t)((int16_t)src)*(int32_t)((int16_t)tmp); - I.CarryVal = I.OverVal = (((int32_t)dst) >> 15 != 0) && (((int32_t)dst) >> 15 != -1); - RegWord(ModRM)=(int16_t)dst; - CLKM(4,3); + uint32_t tmp; + DEF_r16w; + FETCHWORD(tmp); + dst = (int32_t)((int16_t)src) * (int32_t)((int16_t)tmp); + I.CarryVal = I.OverVal = (((int32_t)dst) >> 15 != 0) && (((int32_t)dst) >> 15 != -1); + RegWord(ModRM) = (int16_t)dst; + CLKM(4, 3); } -OP( 0x6a, i_push_d8 ) + +OP(0x6a, i_push_d8) { - uint32_t tmp = (int16_t)((int16_t)((int8_t)FETCH)); - PUSH(tmp); - CLK(1); + uint32_t tmp = (int16_t)((int16_t)((int8_t)FETCH)); + PUSH(tmp); + CLK(1); } -OP( 0x6b, i_imul_d8 ) + +OP(0x6b, i_imul_d8) { - uint32_t src2; - DEF_r16w; - src2= (int16_t)((int16_t)((int8_t)FETCH)); - dst = (int32_t)((int16_t)src)*(int32_t)((int16_t)src2); - I.CarryVal = I.OverVal = (((int32_t)dst) >> 15 != 0) && (((int32_t)dst) >> 15 != -1); - RegWord(ModRM)=(int16_t)dst; - CLKM(4,3); + uint32_t src2; + DEF_r16w; + src2 = (int16_t)((int16_t)((int8_t)FETCH)); + dst = (int32_t)((int16_t)src) * (int32_t)((int16_t)src2); + I.CarryVal = I.OverVal = (((int32_t)dst) >> 15 != 0) && (((int32_t)dst) >> 15 != -1); + RegWord(ModRM) = (int16_t)dst; + CLKM(4, 3); } -OP( 0x6c, i_insb ) + +OP(0x6c, i_insb) { - PutMemB(ES,I.regs.w[IY],read_port(I.regs.w[DW])); - I.regs.w[IY]+= -2 * I.DF + 1; - CLK(6); + PutMemB(ES, I.regs.w[IY], read_port(I.regs.w[DW])); + I.regs.w[IY] += -2 * I.DF + 1; + CLK(6); } -OP( 0x6d, i_insw ) + +OP(0x6d, i_insw) { - PutMemB(ES,I.regs.w[IY],read_port(I.regs.w[DW])); - PutMemB(ES,(I.regs.w[IY]+1)&0xffff,read_port((I.regs.w[DW]+1)&0xffff)); - I.regs.w[IY]+= -4 * I.DF + 2; - CLK(6); + PutMemB(ES, I.regs.w[IY], read_port(I.regs.w[DW])); + PutMemB(ES, (I.regs.w[IY] + 1) & 0xffff, read_port((I.regs.w[DW] + 1) & 0xffff)); + I.regs.w[IY] += -4 * I.DF + 2; + CLK(6); } -OP( 0x6e, i_outsb ) + +OP(0x6e, i_outsb) { - write_port(I.regs.w[DW],GetMemB(DS,I.regs.w[IX])); - I.regs.w[IX]+= -2 * I.DF + 1; - CLK(7); + write_port(I.regs.w[DW], GetMemB(DS, I.regs.w[IX])); + I.regs.w[IX] += -2 * I.DF + 1; + CLK(7); } -OP( 0x6f, i_outsw ) + +OP(0x6f, i_outsw) { - write_port(I.regs.w[DW],GetMemB(DS,I.regs.w[IX])); - write_port((I.regs.w[DW]+1)&0xffff,GetMemB(DS,(I.regs.w[IX]+1)&0xffff)); - I.regs.w[IX]+= -4 * I.DF + 2; - CLK(7); + write_port(I.regs.w[DW], GetMemB(DS, I.regs.w[IX])); + write_port((I.regs.w[DW] + 1) & 0xffff, GetMemB(DS, (I.regs.w[IX] + 1) & 0xffff)); + I.regs.w[IX] += -4 * I.DF + 2; + CLK(7); } -OP( 0x70, i_jo ) +OP(0x70, i_jo) { - JMP( OF); - CLK(1); + JMP(OF); + CLK(1); } -OP( 0x71, i_jno ) + +OP(0x71, i_jno) { - JMP(!OF); - CLK(1); + JMP(!OF); + CLK(1); } -OP( 0x72, i_jc ) + +OP(0x72, i_jc) { - JMP( CF); - CLK(1); + JMP(CF); + CLK(1); } -OP( 0x73, i_jnc ) + +OP(0x73, i_jnc) { - JMP(!CF); - CLK(1); + JMP(!CF); + CLK(1); } -OP( 0x74, i_jz ) + +OP(0x74, i_jz) { - JMP( ZF); - CLK(1); + JMP(ZF); + CLK(1); } -OP( 0x75, i_jnz ) + +OP(0x75, i_jnz) { - JMP(!ZF); - CLK(1); + JMP(!ZF); + CLK(1); } -OP( 0x76, i_jce ) + +OP(0x76, i_jce) { - JMP(CF || ZF); - CLK(1); + JMP(CF || ZF); + CLK(1); } -OP( 0x77, i_jnce ) + +OP(0x77, i_jnce) { - JMP(!(CF || ZF)); - CLK(1); + JMP(!(CF || ZF)); + CLK(1); } -OP( 0x78, i_js ) + +OP(0x78, i_js) { - JMP( SF); - CLK(1); + JMP(SF); + CLK(1); } -OP( 0x79, i_jns ) + +OP(0x79, i_jns) { - JMP(!SF); - CLK(1); + JMP(!SF); + CLK(1); } -OP( 0x7a, i_jp ) + +OP(0x7a, i_jp) { - JMP( PF); - CLK(1); + JMP(PF); + CLK(1); } -OP( 0x7b, i_jnp ) + +OP(0x7b, i_jnp) { - JMP(!PF); - CLK(1); + JMP(!PF); + CLK(1); } -OP( 0x7c, i_jl ) + +OP(0x7c, i_jl) { - JMP((SF!=OF)&&(!ZF)); - CLK(1); + JMP((SF != OF) && (!ZF)); + CLK(1); } -OP( 0x7d, i_jnl ) + +OP(0x7d, i_jnl) { - JMP((ZF)||(SF==OF)); - CLK(1); + JMP((ZF) || (SF == OF)); + CLK(1); } -OP( 0x7e, i_jle ) + +OP(0x7e, i_jle) { - JMP((ZF)||(SF!=OF)); - CLK(1); + JMP((ZF) || (SF != OF)); + CLK(1); } -OP( 0x7f, i_jnle ) + +OP(0x7f, i_jnle) { - JMP((SF==OF)&&(!ZF)); - CLK(1); + JMP((SF == OF) && (!ZF)); + CLK(1); } -OP( 0x80, i_80pre ) +OP(0x80, i_80pre) { - uint32_t dst, src; - GetModRM; - dst = GetRMByte(ModRM); - src = FETCH; - CLKM(3,1) + uint32_t dst, src; + GetModRM; + dst = GetRMByte(ModRM); + src = FETCH; + CLKM(3, 1) - switch (ModRM & 0x38) - { - case 0x00: - ADDB; - PutbackRMByte(ModRM,dst); - break; + switch (ModRM & 0x38) + { + case 0x00: + ADDB; + PutbackRMByte(ModRM, dst); + break; - case 0x08: - ORB; - PutbackRMByte(ModRM,dst); - break; + case 0x08: + ORB; + PutbackRMByte(ModRM, dst); + break; - case 0x10: - src+=CF; - ADDB; - PutbackRMByte(ModRM,dst); - break; + case 0x10: + src += CF; + ADDB; + PutbackRMByte(ModRM, dst); + break; - case 0x18: - src+=CF; - SUBB; - PutbackRMByte(ModRM,dst); - break; + case 0x18: + src += CF; + SUBB; + PutbackRMByte(ModRM, dst); + break; - case 0x20: - ANDB; - PutbackRMByte(ModRM,dst); - break; + case 0x20: + ANDB; + PutbackRMByte(ModRM, dst); + break; - case 0x28: - SUBB; - PutbackRMByte(ModRM,dst); - break; + case 0x28: + SUBB; + PutbackRMByte(ModRM, dst); + break; - case 0x30: - XORB; - PutbackRMByte(ModRM,dst); - break; + case 0x30: + XORB; + PutbackRMByte(ModRM, dst); + break; - case 0x38: - SUBB; - break; /* CMP */ - } + case 0x38: + SUBB; + break; /* CMP */ + } } -OP( 0x81, i_81pre ) +OP(0x81, i_81pre) { - uint32_t dst, src; - GetModRM; - dst = GetRMWord(ModRM); - src = FETCH; - src+= (FETCH << 8); - CLKM(3,1) + uint32_t dst, src; + GetModRM; + dst = GetRMWord(ModRM); + src = FETCH; + src += (FETCH << 8); + CLKM(3, 1) - switch (ModRM & 0x38) - { - case 0x00: - ADDW; - PutbackRMWord(ModRM,dst); - break; - - case 0x08: - ORW; - PutbackRMWord(ModRM,dst); - break; - - case 0x10: - src+=CF; - ADDW; - PutbackRMWord(ModRM,dst); - break; - - case 0x18: - src+=CF; - SUBW; - PutbackRMWord(ModRM,dst); - break; - - case 0x20: - ANDW; - PutbackRMWord(ModRM,dst); - break; - - case 0x28: - SUBW; - PutbackRMWord(ModRM,dst); - break; - - case 0x30: - XORW; - PutbackRMWord(ModRM,dst); - break; - - case 0x38: - SUBW; - break; /* CMP */ - } -} - -OP( 0x82, i_82pre ) -{ - uint32_t dst, src; - GetModRM; - dst = GetRMByte(ModRM); - src = (uint8_t)((int8_t)FETCH); - CLKM(3,1) - - switch (ModRM & 0x38) - { - case 0x00: - ADDB; - PutbackRMByte(ModRM,dst); - break; - - case 0x08: - ORB; - PutbackRMByte(ModRM,dst); - break; - - case 0x10: - src+=CF; - ADDB; - PutbackRMByte(ModRM,dst); - break; - - case 0x18: - src+=CF; - SUBB; - PutbackRMByte(ModRM,dst); - break; - - case 0x20: - ANDB; - PutbackRMByte(ModRM,dst); - break; - - case 0x28: - SUBB; - PutbackRMByte(ModRM,dst); - break; - - case 0x30: - XORB; - PutbackRMByte(ModRM,dst); - break; - - case 0x38: - SUBB; - break; /* CMP */ - } -} - -OP( 0x83, i_83pre ) -{ - uint32_t dst, src; - GetModRM; - dst = GetRMWord(ModRM); - src = (int16_t)((int16_t)((int8_t)FETCH)); - CLKM(3,1) - - switch (ModRM & 0x38) - { - case 0x00: - ADDW; - PutbackRMWord(ModRM,dst); - break; - - case 0x08: - ORW; - PutbackRMWord(ModRM,dst); - break; - - case 0x10: - src+=CF; - ADDW; - PutbackRMWord(ModRM,dst); - break; - - case 0x18: - src+=CF; - SUBW; - PutbackRMWord(ModRM,dst); - break; - - case 0x20: - ANDW; - PutbackRMWord(ModRM,dst); - break; - - case 0x28: - SUBW; - PutbackRMWord(ModRM,dst); - break; - - case 0x30: - XORW; - PutbackRMWord(ModRM,dst); - break; - - case 0x38: - SUBW; - break; /* CMP */ - } -} - -OP( 0x84, i_test_br8 ) -{ - DEF_br8; - ANDB; - CLKM(2,1); + switch (ModRM & 0x38) + { + case 0x00: + ADDW; + PutbackRMWord(ModRM, dst); + break; + + case 0x08: + ORW; + PutbackRMWord(ModRM, dst); + break; + + case 0x10: + src += CF; + ADDW; + PutbackRMWord(ModRM, dst); + break; + + case 0x18: + src += CF; + SUBW; + PutbackRMWord(ModRM, dst); + break; + + case 0x20: + ANDW; + PutbackRMWord(ModRM, dst); + break; + + case 0x28: + SUBW; + PutbackRMWord(ModRM, dst); + break; + + case 0x30: + XORW; + PutbackRMWord(ModRM, dst); + break; + + case 0x38: + SUBW; + break; /* CMP */ + } +} + +OP(0x82, i_82pre) +{ + uint32_t dst, src; + GetModRM; + dst = GetRMByte(ModRM); + src = (uint8_t)((int8_t)FETCH); + CLKM(3, 1) + + switch (ModRM & 0x38) + { + case 0x00: + ADDB; + PutbackRMByte(ModRM, dst); + break; + + case 0x08: + ORB; + PutbackRMByte(ModRM, dst); + break; + + case 0x10: + src += CF; + ADDB; + PutbackRMByte(ModRM, dst); + break; + + case 0x18: + src += CF; + SUBB; + PutbackRMByte(ModRM, dst); + break; + + case 0x20: + ANDB; + PutbackRMByte(ModRM, dst); + break; + + case 0x28: + SUBB; + PutbackRMByte(ModRM, dst); + break; + + case 0x30: + XORB; + PutbackRMByte(ModRM, dst); + break; + + case 0x38: + SUBB; + break; /* CMP */ + } +} + +OP(0x83, i_83pre) +{ + uint32_t dst, src; + GetModRM; + dst = GetRMWord(ModRM); + src = (int16_t)((int16_t)((int8_t)FETCH)); + CLKM(3, 1) + + switch (ModRM & 0x38) + { + case 0x00: + ADDW; + PutbackRMWord(ModRM, dst); + break; + + case 0x08: + ORW; + PutbackRMWord(ModRM, dst); + break; + + case 0x10: + src += CF; + ADDW; + PutbackRMWord(ModRM, dst); + break; + + case 0x18: + src += CF; + SUBW; + PutbackRMWord(ModRM, dst); + break; + + case 0x20: + ANDW; + PutbackRMWord(ModRM, dst); + break; + + case 0x28: + SUBW; + PutbackRMWord(ModRM, dst); + break; + + case 0x30: + XORW; + PutbackRMWord(ModRM, dst); + break; + + case 0x38: + SUBW; + break; /* CMP */ + } +} + +OP(0x84, i_test_br8) +{ + DEF_br8; + ANDB; + CLKM(2, 1); } -OP( 0x85, i_test_wr16 ) + +OP(0x85, i_test_wr16) { - DEF_wr16; - ANDW; - CLKM(2,1); + DEF_wr16; + ANDW; + CLKM(2, 1); } -OP( 0x86, i_xchg_br8 ) + +OP(0x86, i_xchg_br8) { - DEF_br8; - RegByte(ModRM)=dst; - PutbackRMByte(ModRM,src); - CLKM(5,3); + DEF_br8; + RegByte(ModRM) = dst; + PutbackRMByte(ModRM, src); + CLKM(5, 3); } -OP( 0x87, i_xchg_wr16 ) + +OP(0x87, i_xchg_wr16) { - DEF_wr16; - RegWord(ModRM)=dst; - PutbackRMWord(ModRM,src); - CLKM(5,3); + DEF_wr16; + RegWord(ModRM) = dst; + PutbackRMWord(ModRM, src); + CLKM(5, 3); } -OP( 0x88, i_mov_br8 ) +OP(0x88, i_mov_br8) { - uint8_t src; - GetModRM; - src = RegByte(ModRM); - PutRMByte(ModRM,src); - CLKM(1,1); + uint8_t src; + GetModRM; + src = RegByte(ModRM); + PutRMByte(ModRM, src); + CLKM(1, 1); } -OP( 0x89, i_mov_wr16 ) + +OP(0x89, i_mov_wr16) { - uint16_t src; - GetModRM; - src = RegWord(ModRM); - PutRMWord(ModRM,src); - CLKM(1,1); + uint16_t src; + GetModRM; + src = RegWord(ModRM); + PutRMWord(ModRM, src); + CLKM(1, 1); } -OP( 0x8a, i_mov_r8b ) + +OP(0x8a, i_mov_r8b) { - uint8_t src; - GetModRM; - src = GetRMByte(ModRM); - RegByte(ModRM)=src; - CLKM(1,1); + uint8_t src; + GetModRM; + src = GetRMByte(ModRM); + RegByte(ModRM) = src; + CLKM(1, 1); } -OP( 0x8b, i_mov_r16w ) + +OP(0x8b, i_mov_r16w) { - uint16_t src; - GetModRM; - src = GetRMWord(ModRM); - RegWord(ModRM)=src; - CLKM(1,1); + uint16_t src; + GetModRM; + src = GetRMWord(ModRM); + RegWord(ModRM) = src; + CLKM(1, 1); } -OP( 0x8c, i_mov_wsreg ) + +OP(0x8c, i_mov_wsreg) { - GetModRM; - PutRMWord(ModRM,I.sregs[(ModRM & 0x38) >> 3]); - CLKM(1,1); + GetModRM; + PutRMWord(ModRM, I.sregs[(ModRM & 0x38) >> 3]); + CLKM(1, 1); } -OP( 0x8d, i_lea ) + +OP(0x8d, i_lea) { - uint16_t ModRM = FETCH; - (void)(*GetEA[ModRM])(); - RegWord(ModRM)=EO; - CLK(1); + uint16_t ModRM = FETCH; + (void)(*GetEA[ModRM])(); + RegWord(ModRM) = EO; + CLK(1); } -OP( 0x8e, i_mov_sregw ) + +OP(0x8e, i_mov_sregw) { - uint16_t src; - GetModRM; - src = GetRMWord(ModRM); - CLKM(3,2); + uint16_t src; + GetModRM; + src = GetRMWord(ModRM); + CLKM(3, 2); - switch (ModRM & 0x38) - { - case 0x00: - I.sregs[ES] = src; - break; /* mov es,ew */ + switch (ModRM & 0x38) + { + case 0x00: + I.sregs[ES] = src; + break; /* mov es,ew */ - case 0x08: - I.sregs[CS] = src; - break; /* mov cs,ew */ + case 0x08: + I.sregs[CS] = src; + break; /* mov cs,ew */ - case 0x10: - I.sregs[SS] = src; - break; /* mov ss,ew */ + case 0x10: + I.sregs[SS] = src; + break; /* mov ss,ew */ - case 0x18: - I.sregs[DS] = src; - break; /* mov ds,ew */ + case 0x18: + I.sregs[DS] = src; + break; /* mov ds,ew */ - default: - ; - } + default:; + } - no_interrupt=1; + no_interrupt = 1; } -OP( 0x8f, i_popw ) + +OP(0x8f, i_popw) { - uint16_t tmp; - GetModRM; - POP(tmp); - PutRMWord(ModRM,tmp); - CLKM(3,1); + uint16_t tmp; + GetModRM; + POP(tmp); + PutRMWord(ModRM, tmp); + CLKM(3, 1); } -OP( 0x90, i_nop ) + +OP(0x90, i_nop) { - CLK(1); + CLK(1); - /* Cycle skip for idle loops (0: NOP 1: JMP 0) */ - if (no_interrupt==0 && nec_ICount>0 && (PEEKOP((I.sregs[CS]<<4)+I.ip))==0xeb && (PEEK((I.sregs[CS]<<4)+I.ip+1))==0xfd) - { - nec_ICount%=15; - } + /* Cycle skip for idle loops (0: NOP 1: JMP 0) */ + if (no_interrupt == 0 && nec_ICount > 0 && (PEEKOP((I.sregs[CS] << 4) + I.ip)) == 0xeb && + (PEEK((I.sregs[CS] << 4) + I.ip + 1)) == 0xfd) + { + nec_ICount %= 15; + } } -OP( 0x91, i_xchg_axcx ) + +OP(0x91, i_xchg_axcx) +{ + XchgAWReg(CW); + CLK(3); +} + +OP(0x92, i_xchg_axdx) { - XchgAWReg(CW); - CLK(3); + XchgAWReg(DW); + CLK(3); } -OP( 0x92, i_xchg_axdx ) + +OP(0x93, i_xchg_axbx) { - XchgAWReg(DW); - CLK(3); + XchgAWReg(BW); + CLK(3); } -OP( 0x93, i_xchg_axbx ) + +OP(0x94, i_xchg_axsp) { - XchgAWReg(BW); - CLK(3); + XchgAWReg(SP); + CLK(3); } -OP( 0x94, i_xchg_axsp ) + +OP(0x95, i_xchg_axbp) { - XchgAWReg(SP); - CLK(3); + XchgAWReg(BP); + CLK(3); } -OP( 0x95, i_xchg_axbp ) + +OP(0x96, i_xchg_axsi) { - XchgAWReg(BP); - CLK(3); + XchgAWReg(IX); + CLK(3); } -OP( 0x96, i_xchg_axsi ) + +OP(0x97, i_xchg_axdi) { - XchgAWReg(IX); - CLK(3); + XchgAWReg(IY); + CLK(3); } -OP( 0x97, i_xchg_axdi ) + +OP(0x98, i_cbw) { - XchgAWReg(IY); - CLK(3); + I.regs.b[AH] = (I.regs.b[AL] & 0x80) ? 0xff : 0; + CLK(1); } -OP( 0x98, i_cbw ) +OP(0x99, i_cwd) { - I.regs.b[AH] = (I.regs.b[AL] & 0x80) ? 0xff : 0; - CLK(1); + I.regs.w[DW] = (I.regs.b[AH] & 0x80) ? 0xffff : 0; + CLK(1); } -OP( 0x99, i_cwd ) + +OP(0x9a, i_call_far) { - I.regs.w[DW] = (I.regs.b[AH] & 0x80) ? 0xffff : 0; - CLK(1); + uint32_t tmp, tmp2; + FETCHWORD(tmp); + FETCHWORD(tmp2); + PUSH(I.sregs[CS]); + PUSH(I.ip); + I.ip = (int16_t)tmp; + I.sregs[CS] = (int16_t)tmp2; + CLK(10); } -OP( 0x9a, i_call_far ) + +OP(0x9b, i_wait) { - uint32_t tmp, tmp2; - FETCHWORD(tmp); - FETCHWORD(tmp2); - PUSH(I.sregs[CS]); - PUSH(I.ip); - I.ip = (int16_t)tmp; - I.sregs[CS] = (int16_t)tmp2; - CLK(10); + ; } -OP( 0x9b, i_wait ) { ; } -OP( 0x9c, i_pushf ) + +OP(0x9c, i_pushf) { - PUSH( CompressFlags() ); - CLK(2); + PUSH(CompressFlags()); + CLK(2); } -OP( 0x9d, i_popf ) + +OP(0x9d, i_popf) { - uint32_t tmp; - POP(tmp); - ExpandFlags(tmp); - CLK(3); + uint32_t tmp; + POP(tmp); + ExpandFlags(tmp); + CLK(3); } -OP( 0x9e, i_sahf ) + +OP(0x9e, i_sahf) { - uint32_t tmp = (CompressFlags() & 0xff00) | (I.regs.b[AH] & 0xd5); - ExpandFlags(tmp); - CLK(4); + uint32_t tmp = (CompressFlags() & 0xff00) | (I.regs.b[AH] & 0xd5); + ExpandFlags(tmp); + CLK(4); } -OP( 0x9f, i_lahf ) + +OP(0x9f, i_lahf) { - I.regs.b[AH] = CompressFlags() & 0xff; - CLK(2); + I.regs.b[AH] = CompressFlags() & 0xff; + CLK(2); } -OP( 0xa0, i_mov_aldisp ) +OP(0xa0, i_mov_aldisp) { - uint32_t addr; - FETCHWORD(addr); - I.regs.b[AL] = GetMemB(DS, addr); - CLK(1); + uint32_t addr; + FETCHWORD(addr); + I.regs.b[AL] = GetMemB(DS, addr); + CLK(1); } -OP( 0xa1, i_mov_axdisp ) + +OP(0xa1, i_mov_axdisp) { - uint32_t addr; - FETCHWORD(addr); - I.regs.b[AL] = GetMemB(DS, addr); - I.regs.b[AH] = GetMemB(DS, (addr+1)&0xffff); - CLK(1); + uint32_t addr; + FETCHWORD(addr); + I.regs.b[AL] = GetMemB(DS, addr); + I.regs.b[AH] = GetMemB(DS, (addr + 1) & 0xffff); + CLK(1); } -OP( 0xa2, i_mov_dispal ) + +OP(0xa2, i_mov_dispal) { - uint32_t addr; - FETCHWORD(addr); - PutMemB(DS, addr, I.regs.b[AL]); - CLK(1); + uint32_t addr; + FETCHWORD(addr); + PutMemB(DS, addr, I.regs.b[AL]); + CLK(1); } -OP( 0xa3, i_mov_dispax ) + +OP(0xa3, i_mov_dispax) { - uint32_t addr; - FETCHWORD(addr); - PutMemB(DS, addr, I.regs.b[AL]); - PutMemB(DS, (addr+1)&0xffff, I.regs.b[AH]); - CLK(1); + uint32_t addr; + FETCHWORD(addr); + PutMemB(DS, addr, I.regs.b[AL]); + PutMemB(DS, (addr + 1) & 0xffff, I.regs.b[AH]); + CLK(1); } -OP( 0xa4, i_movsb ) + +OP(0xa4, i_movsb) { - uint32_t tmp = GetMemB(DS,I.regs.w[IX]); - PutMemB(ES,I.regs.w[IY], tmp); - I.regs.w[IY] += -2 * I.DF + 1; - I.regs.w[IX] += -2 * I.DF + 1; - CLK(5); + uint32_t tmp = GetMemB(DS, I.regs.w[IX]); + PutMemB(ES, I.regs.w[IY], tmp); + I.regs.w[IY] += -2 * I.DF + 1; + I.regs.w[IX] += -2 * I.DF + 1; + CLK(5); } -OP( 0xa5, i_movsw ) + +OP(0xa5, i_movsw) { - uint32_t tmp = GetMemW(DS,I.regs.w[IX]); - PutMemW(ES,I.regs.w[IY], tmp); - I.regs.w[IY] += -4 * I.DF + 2; - I.regs.w[IX] += -4 * I.DF + 2; - CLK(5); + uint32_t tmp = GetMemW(DS, I.regs.w[IX]); + PutMemW(ES, I.regs.w[IY], tmp); + I.regs.w[IY] += -4 * I.DF + 2; + I.regs.w[IX] += -4 * I.DF + 2; + CLK(5); } -OP( 0xa6, i_cmpsb ) + +OP(0xa6, i_cmpsb) { - uint32_t src = GetMemB(ES, I.regs.w[IY]); - uint32_t dst = GetMemB(DS, I.regs.w[IX]); - SUBB; - I.regs.w[IY] += -2 * I.DF + 1; - I.regs.w[IX] += -2 * I.DF + 1; - CLK(6); + uint32_t src = GetMemB(ES, I.regs.w[IY]); + uint32_t dst = GetMemB(DS, I.regs.w[IX]); + SUBB; + I.regs.w[IY] += -2 * I.DF + 1; + I.regs.w[IX] += -2 * I.DF + 1; + CLK(6); } -OP( 0xa7, i_cmpsw ) + +OP(0xa7, i_cmpsw) { - uint32_t src = GetMemW(ES, I.regs.w[IY]); - uint32_t dst = GetMemW(DS, I.regs.w[IX]); - SUBW; - I.regs.w[IY] += -4 * I.DF + 2; - I.regs.w[IX] += -4 * I.DF + 2; - CLK(6); + uint32_t src = GetMemW(ES, I.regs.w[IY]); + uint32_t dst = GetMemW(DS, I.regs.w[IX]); + SUBW; + I.regs.w[IY] += -4 * I.DF + 2; + I.regs.w[IX] += -4 * I.DF + 2; + CLK(6); } -OP( 0xa8, i_test_ald8 ) +OP(0xa8, i_test_ald8) { - DEF_ald8; - ANDB; - CLK(1); + DEF_ald8; + ANDB; + CLK(1); } -OP( 0xa9, i_test_axd16 ) + +OP(0xa9, i_test_axd16) { - DEF_axd16; - ANDW; - CLK(1); + DEF_axd16; + ANDW; + CLK(1); } -OP( 0xaa, i_stosb ) + +OP(0xaa, i_stosb) { - PutMemB(ES,I.regs.w[IY],I.regs.b[AL]); - I.regs.w[IY] += -2 * I.DF + 1; - CLK(3); + PutMemB(ES, I.regs.w[IY], I.regs.b[AL]); + I.regs.w[IY] += -2 * I.DF + 1; + CLK(3); } -OP( 0xab, i_stosw ) + +OP(0xab, i_stosw) { - PutMemW(ES,I.regs.w[IY],I.regs.w[AW]); - I.regs.w[IY] += -4 * I.DF + 2; - CLK(3); + PutMemW(ES, I.regs.w[IY], I.regs.w[AW]); + I.regs.w[IY] += -4 * I.DF + 2; + CLK(3); } -OP( 0xac, i_lodsb ) + +OP(0xac, i_lodsb) { - I.regs.b[AL] = GetMemB(DS,I.regs.w[IX]); - I.regs.w[IX] += -2 * I.DF + 1; - CLK(3); + I.regs.b[AL] = GetMemB(DS, I.regs.w[IX]); + I.regs.w[IX] += -2 * I.DF + 1; + CLK(3); } -OP( 0xad, i_lodsw ) + +OP(0xad, i_lodsw) { - I.regs.w[AW] = GetMemW(DS,I.regs.w[IX]); - I.regs.w[IX] += -4 * I.DF + 2; - CLK(3); + I.regs.w[AW] = GetMemW(DS, I.regs.w[IX]); + I.regs.w[IX] += -4 * I.DF + 2; + CLK(3); } -OP( 0xae, i_scasb ) + +OP(0xae, i_scasb) { - uint32_t src = GetMemB(ES, I.regs.w[IY]); - uint32_t dst = I.regs.b[AL]; - SUBB; - I.regs.w[IY] += -2 * I.DF + 1; - CLK(4); + uint32_t src = GetMemB(ES, I.regs.w[IY]); + uint32_t dst = I.regs.b[AL]; + SUBB; + I.regs.w[IY] += -2 * I.DF + 1; + CLK(4); } -OP( 0xaf, i_scasw ) + +OP(0xaf, i_scasw) { - uint32_t src = GetMemW(ES, I.regs.w[IY]); - uint32_t dst = I.regs.w[AW]; - SUBW; - I.regs.w[IY] += -4 * I.DF + 2; - CLK(4); + uint32_t src = GetMemW(ES, I.regs.w[IY]); + uint32_t dst = I.regs.w[AW]; + SUBW; + I.regs.w[IY] += -4 * I.DF + 2; + CLK(4); } -OP( 0xb0, i_mov_ald8 ) +OP(0xb0, i_mov_ald8) { - I.regs.b[AL] = FETCH; - CLK(1); + I.regs.b[AL] = FETCH; + CLK(1); } -OP( 0xb1, i_mov_cld8 ) + +OP(0xb1, i_mov_cld8) { - I.regs.b[CL] = FETCH; - CLK(1); + I.regs.b[CL] = FETCH; + CLK(1); } -OP( 0xb2, i_mov_dld8 ) + +OP(0xb2, i_mov_dld8) { - I.regs.b[DL] = FETCH; - CLK(1); + I.regs.b[DL] = FETCH; + CLK(1); } -OP( 0xb3, i_mov_bld8 ) + +OP(0xb3, i_mov_bld8) { - I.regs.b[BL] = FETCH; - CLK(1); + I.regs.b[BL] = FETCH; + CLK(1); } -OP( 0xb4, i_mov_ahd8 ) + +OP(0xb4, i_mov_ahd8) { - I.regs.b[AH] = FETCH; - CLK(1); + I.regs.b[AH] = FETCH; + CLK(1); } -OP( 0xb5, i_mov_chd8 ) + +OP(0xb5, i_mov_chd8) { - I.regs.b[CH] = FETCH; - CLK(1); + I.regs.b[CH] = FETCH; + CLK(1); } -OP( 0xb6, i_mov_dhd8 ) + +OP(0xb6, i_mov_dhd8) { - I.regs.b[DH] = FETCH; - CLK(1); + I.regs.b[DH] = FETCH; + CLK(1); } -OP( 0xb7, i_mov_bhd8 ) + +OP(0xb7, i_mov_bhd8) { - I.regs.b[BH] = FETCH; - CLK(1); + I.regs.b[BH] = FETCH; + CLK(1); } -OP( 0xb8, i_mov_axd16 ) +OP(0xb8, i_mov_axd16) { - I.regs.b[AL] = FETCH; - I.regs.b[AH] = FETCH; - CLK(1); + I.regs.b[AL] = FETCH; + I.regs.b[AH] = FETCH; + CLK(1); } -OP( 0xb9, i_mov_cxd16 ) + +OP(0xb9, i_mov_cxd16) { - I.regs.b[CL] = FETCH; - I.regs.b[CH] = FETCH; - CLK(1); + I.regs.b[CL] = FETCH; + I.regs.b[CH] = FETCH; + CLK(1); } -OP( 0xba, i_mov_dxd16 ) + +OP(0xba, i_mov_dxd16) { - I.regs.b[DL] = FETCH; - I.regs.b[DH] = FETCH; - CLK(1); + I.regs.b[DL] = FETCH; + I.regs.b[DH] = FETCH; + CLK(1); } -OP( 0xbb, i_mov_bxd16 ) + +OP(0xbb, i_mov_bxd16) { - I.regs.b[BL] = FETCH; - I.regs.b[BH] = FETCH; - CLK(1); + I.regs.b[BL] = FETCH; + I.regs.b[BH] = FETCH; + CLK(1); } -OP( 0xbc, i_mov_spd16 ) + +OP(0xbc, i_mov_spd16) { - I.regs.b[SPL] = FETCH; - I.regs.b[SPH] = FETCH; - CLK(1); + I.regs.b[SPL] = FETCH; + I.regs.b[SPH] = FETCH; + CLK(1); } -OP( 0xbd, i_mov_bpd16 ) + +OP(0xbd, i_mov_bpd16) { - I.regs.b[BPL] = FETCH; - I.regs.b[BPH] = FETCH; - CLK(1); + I.regs.b[BPL] = FETCH; + I.regs.b[BPH] = FETCH; + CLK(1); } -OP( 0xbe, i_mov_sid16 ) + +OP(0xbe, i_mov_sid16) { - I.regs.b[IXL] = FETCH; - I.regs.b[IXH] = FETCH; - CLK(1); + I.regs.b[IXL] = FETCH; + I.regs.b[IXH] = FETCH; + CLK(1); } -OP( 0xbf, i_mov_did16 ) + +OP(0xbf, i_mov_did16) { - I.regs.b[IYL] = FETCH; - I.regs.b[IYH] = FETCH; - CLK(1); + I.regs.b[IYL] = FETCH; + I.regs.b[IYH] = FETCH; + CLK(1); } -OP( 0xc0, i_rotshft_bd8 ) +OP(0xc0, i_rotshft_bd8) { - uint32_t src, dst; - uint8_t c; - GetModRM; - src = (uint32_t)GetRMByte(ModRM); - dst=src; - c=FETCH; - c&=0x1f; - CLKM(5,3); + uint32_t src, dst; + uint8_t c; + GetModRM; + src = (uint32_t)GetRMByte(ModRM); + dst = src; + c = FETCH; + c &= 0x1f; + CLKM(5, 3); + + if (c) + { + switch (ModRM & 0x38) + { + case 0x00: + do + { + ROL_BYTE; + c--; + } while (c > 0); + + PutbackRMByte(ModRM, (uint8_t)dst); + break; + + case 0x08: + do + { + ROR_BYTE; + c--; + } while (c > 0); + + PutbackRMByte(ModRM, (uint8_t)dst); + break; - if (c) switch (ModRM & 0x38) - { - case 0x00: - do - { - ROL_BYTE; - c--; - } - while (c>0); + case 0x10: + do + { + ROLC_BYTE; + c--; + } while (c > 0); - PutbackRMByte(ModRM,(uint8_t)dst); - break; + PutbackRMByte(ModRM, (uint8_t)dst); + break; - case 0x08: - do - { - ROR_BYTE; - c--; - } - while (c>0); + case 0x18: + do + { + RORC_BYTE; + c--; + } while (c > 0); - PutbackRMByte(ModRM,(uint8_t)dst); - break; + PutbackRMByte(ModRM, (uint8_t)dst); + break; - case 0x10: - do - { - ROLC_BYTE; - c--; - } - while (c>0); - - PutbackRMByte(ModRM,(uint8_t)dst); - break; - - case 0x18: - do - { - RORC_BYTE; - c--; - } - while (c>0); - - PutbackRMByte(ModRM,(uint8_t)dst); - break; - - case 0x20: - SHL_BYTE(c); - I.AuxVal = 1; - break;// - - case 0x28: - SHR_BYTE(c); - I.AuxVal = 1; - break;// - - case 0x30: - break; - - case 0x38: - SHRA_BYTE(c); - break; - } -} - -OP( 0xc1, i_rotshft_wd8 ) -{ - uint32_t src, dst; - uint8_t c; - GetModRM; - src = (uint32_t)GetRMWord(ModRM); - dst=src; - c=FETCH; - c&=0x1f; - CLKM(5,3); - - if (c) switch (ModRM & 0x38) - { - case 0x00: - do - { - ROL_WORD; - c--; - } - while (c>0); - - PutbackRMWord(ModRM,(int16_t)dst); - break; - - case 0x08: - do - { - ROR_WORD; - c--; - } - while (c>0); - - PutbackRMWord(ModRM,(int16_t)dst); - break; - - case 0x10: - do - { - ROLC_WORD; - c--; - } - while (c>0); - - PutbackRMWord(ModRM,(int16_t)dst); - break; - - case 0x18: - do - { - RORC_WORD; - c--; - } - while (c>0); - - PutbackRMWord(ModRM,(int16_t)dst); - break; - - case 0x20: - SHL_WORD(c); - I.AuxVal = 1; - break; - - case 0x28: - SHR_WORD(c); - I.AuxVal = 1; - break; + case 0x20: + SHL_BYTE(c); + I.AuxVal = 1; + break;// - case 0x30: - break; + case 0x28: + SHR_BYTE(c); + I.AuxVal = 1; + break;// - case 0x38: - SHRA_WORD(c); - break; - } + case 0x30: + break; + + case 0x38: + SHRA_BYTE(c); + break; + } + } +} + +OP(0xc1, i_rotshft_wd8) +{ + uint32_t src, dst; + uint8_t c; + GetModRM; + src = (uint32_t)GetRMWord(ModRM); + dst = src; + c = FETCH; + c &= 0x1f; + CLKM(5, 3); + + if (c) + { + switch (ModRM & 0x38) + { + case 0x00: + do + { + ROL_WORD; + c--; + } while (c > 0); + + PutbackRMWord(ModRM, (int16_t)dst); + break; + + case 0x08: + do + { + ROR_WORD; + c--; + } while (c > 0); + + PutbackRMWord(ModRM, (int16_t)dst); + break; + + case 0x10: + do + { + ROLC_WORD; + c--; + } while (c > 0); + + PutbackRMWord(ModRM, (int16_t)dst); + break; + + case 0x18: + do + { + RORC_WORD; + c--; + } while (c > 0); + + PutbackRMWord(ModRM, (int16_t)dst); + break; + + case 0x20: + SHL_WORD(c); + I.AuxVal = 1; + break; + + case 0x28: + SHR_WORD(c); + I.AuxVal = 1; + break; + + case 0x30: + break; + + case 0x38: + SHRA_WORD(c); + break; + } + } +} + +OP(0xc2, i_ret_d16) +{ + uint32_t count = FETCH; + count += FETCH << 8; + POP(I.ip); + I.regs.w[SP] += count; + CLK(6); } -OP( 0xc2, i_ret_d16 ) +OP(0xc3, i_ret) { - uint32_t count = FETCH; - count += FETCH << 8; - POP(I.ip); - I.regs.w[SP]+=count; - CLK(6); + POP(I.ip); + CLK(6); } -OP( 0xc3, i_ret ) + +OP(0xc4, i_les_dw) { - POP(I.ip); - CLK(6); + GetModRM; + int16_t tmp = GetRMWord(ModRM); + RegWord(ModRM) = tmp; + I.sregs[ES] = GetnextRMWord; + CLK(6); } -OP( 0xc4, i_les_dw ) + +OP(0xc5, i_lds_dw) { - GetModRM; - int16_t tmp = GetRMWord(ModRM); - RegWord(ModRM)=tmp; - I.sregs[ES] = GetnextRMWord; - CLK(6); + GetModRM; + int16_t tmp = GetRMWord(ModRM); + RegWord(ModRM) = tmp; + I.sregs[DS] = GetnextRMWord; + CLK(6); } -OP( 0xc5, i_lds_dw ) + +OP(0xc6, i_mov_bd8) { - GetModRM; - int16_t tmp = GetRMWord(ModRM); - RegWord(ModRM)=tmp; - I.sregs[DS] = GetnextRMWord; - CLK(6); -} -OP( 0xc6, i_mov_bd8 ) -{ - GetModRM; - PutImmRMByte(ModRM); - CLK(1); + GetModRM; + PutImmRMByte(ModRM); + CLK(1); } -OP( 0xc7, i_mov_wd16 ) -{ - GetModRM; - PutImmRMWord(ModRM); - CLK(1); + +OP(0xc7, i_mov_wd16) +{ + GetModRM; + PutImmRMWord(ModRM); + CLK(1); } -OP( 0xc8, i_enter ) +OP(0xc8, i_enter) { - uint32_t nb = FETCH; - uint32_t i,level; + uint32_t nb = FETCH; + uint32_t i, level; + + CLK(19); + nb += FETCH << 8; + level = FETCH; + PUSH(I.regs.w[BP]); + I.regs.w[BP] = I.regs.w[SP]; + I.regs.w[SP] -= nb; - CLK(19); - nb += FETCH << 8; - level = FETCH; - PUSH(I.regs.w[BP]); - I.regs.w[BP]=I.regs.w[SP]; - I.regs.w[SP] -= nb; + for (i = 1 ; i < level ; i++) + { + PUSH(GetMemW(SS, I.regs.w[BP] - i * 2)); + CLK(4); + } - for (i=1; i0); - - PutbackRMByte(ModRM,(uint8_t)dst); - break; - - case 0x08: - do - { - ROR_BYTE; - c--; - CLK(1); - } - while (c>0); - - PutbackRMByte(ModRM,(uint8_t)dst); - break; - - case 0x10: - do - { - ROLC_BYTE; - c--; - CLK(1); - } - while (c>0); - - PutbackRMByte(ModRM,(uint8_t)dst); - break; - - case 0x18: - do - { - RORC_BYTE; - c--; - CLK(1); - } - while (c>0); - - PutbackRMByte(ModRM,(uint8_t)dst); - break; - - case 0x20: - SHL_BYTE(c); - I.AuxVal = 1; - break; - - case 0x28: - SHR_BYTE(c); - I.AuxVal = 1; - break; - - case 0x30: - break; - - case 0x38: - SHRA_BYTE(c); - break; - } -} - -OP( 0xd3, i_rotshft_wcl ) -{ - uint32_t src, dst; - uint8_t c; - GetModRM; - src = (uint32_t)GetRMWord(ModRM); - dst=src; - c=I.regs.b[CL]; - c&=0x1f; - CLKM(5,3); - - if (c) switch (ModRM & 0x38) - { - case 0x00: - do - { - ROL_WORD; - c--; - CLK(1); - } - while (c>0); - - PutbackRMWord(ModRM,(int16_t)dst); - break; - - case 0x08: - do - { - ROR_WORD; - c--; - CLK(1); - } - while (c>0); - - PutbackRMWord(ModRM,(int16_t)dst); - break; - - case 0x10: - do - { - ROLC_WORD; - c--; - CLK(1); - } - while (c>0); - - PutbackRMWord(ModRM,(int16_t)dst); - break; - - case 0x18: - do - { - RORC_WORD; - c--; - CLK(1); - } - while (c>0); - - PutbackRMWord(ModRM,(int16_t)dst); - break; - - case 0x20: - SHL_WORD(c); - I.AuxVal = 1; - break; - - case 0x28: - SHR_WORD(c); - I.AuxVal = 1; - break; - - case 0x30: - break; - - case 0x38: - SHRA_WORD(c); - break; - } -} - -OP( 0xd4, i_aam ) -{ - /*uint32_t mult=FETCH; mult=0;*/ I.regs.b[AH] = I.regs.b[AL] / 10; - I.regs.b[AL] %= 10; - SetSZPF_Word(I.regs.w[AW]); - CLK(17); -} -OP( 0xd5, i_aad ) -{ - /*uint32_t mult=FETCH; mult=0;*/ I.regs.b[AL] = I.regs.b[AH] * 10 + I.regs.b[AL]; - I.regs.b[AH] = 0; - SetSZPF_Byte(I.regs.b[AL]); - CLK(6); -} -OP( 0xd6, i_setalc ) -{ - I.regs.b[AL] = (CF)?0xff:0x00; /* nop at V30MZ? */ - CLK(3); + if (OF) + { + nec_interrupt(4, 0); + CLK(13); + } + else + { + CLK(6); + } } -OP( 0xd7, i_trans ) -{ - uint32_t dest = (I.regs.w[BW]+I.regs.b[AL])&0xffff; - I.regs.b[AL] = GetMemB(DS, dest); - CLK(5); -} -OP( 0xd8, i_fpo ) -{ - /*GetModRM;*/ CLK(3); /* nop at V30MZ? */ -} - -OP( 0xe0, i_loopne ) -{ - int8_t disp = (int8_t)FETCH; - I.regs.w[CW]--; - if (!ZF && I.regs.w[CW]) - { - I.ip = (int16_t)(I.ip+disp); - CLK(6); - } - else - { - CLK(3); - } +OP(0xcf, i_iret) +{ + POP(I.ip); + POP(I.sregs[CS]); + i_popf(); + CLK(10); } -OP( 0xe1, i_loope ) + +OP(0xd0, i_rotshft_b) { - int8_t disp = (int8_t)FETCH; - I.regs.w[CW]--; + uint32_t src, dst; + GetModRM; + src = (uint32_t)GetRMByte(ModRM); + dst = src; + CLKM(3, 1); + + switch (ModRM & 0x38) + { + case 0x00: + ROL_BYTE; + PutbackRMByte(ModRM, (uint8_t)dst); + I.OverVal = (src ^ dst) & 0x80; + break; + + case 0x08: + ROR_BYTE; + PutbackRMByte(ModRM, (uint8_t)dst); + I.OverVal = (src ^ dst) & 0x80; + break; + + case 0x10: + ROLC_BYTE; + PutbackRMByte(ModRM, (uint8_t)dst); + I.OverVal = (src ^ dst) & 0x80; + break; + + case 0x18: + RORC_BYTE; + PutbackRMByte(ModRM, (uint8_t)dst); + I.OverVal = (src ^ dst) & 0x80; + break; + + case 0x20: + SHL_BYTE(1); + I.OverVal = (src ^ dst) & 0x80; + I.AuxVal = 1; + break; + + case 0x28: + SHR_BYTE(1); + I.OverVal = (src ^ dst) & 0x80; + I.AuxVal = 1; + break; + + case 0x30: + break; + + case 0x38: + SHRA_BYTE(1); + I.OverVal = 0; + break; + } +} + +OP(0xd1, i_rotshft_w) +{ + uint32_t src, dst; + GetModRM; + src = (uint32_t)GetRMWord(ModRM); + dst = src; + CLKM(3, 1); + + switch (ModRM & 0x38) + { + case 0x00: + ROL_WORD; + PutbackRMWord(ModRM, (int16_t)dst); + I.OverVal = (src ^ dst) & 0x8000; + break; + + case 0x08: + ROR_WORD; + PutbackRMWord(ModRM, (int16_t)dst); + I.OverVal = (src ^ dst) & 0x8000; + break; + + case 0x10: + ROLC_WORD; + PutbackRMWord(ModRM, (int16_t)dst); + I.OverVal = (src ^ dst) & 0x8000; + break; + + case 0x18: + RORC_WORD; + PutbackRMWord(ModRM, (int16_t)dst); + I.OverVal = (src ^ dst) & 0x8000; + break; + + case 0x20: + SHL_WORD(1); + I.AuxVal = 1; + I.OverVal = (src ^ dst) & 0x8000; + break; + + case 0x28: + SHR_WORD(1); + I.AuxVal = 1; + I.OverVal = (src ^ dst) & 0x8000; + break; + + case 0x30: + break; + + case 0x38: + SHRA_WORD(1); + I.AuxVal = 1; + I.OverVal = 0; + break; + } +} + +OP(0xd2, i_rotshft_bcl) +{ + uint32_t src, dst; + uint8_t c; + GetModRM; + src = (uint32_t)GetRMByte(ModRM); + dst = src; + c = I.regs.b[CL]; + CLKM(5, 3); + c &= 0x1f; + + if (c) + { + switch (ModRM & 0x38) + { + case 0x00: + do + { + ROL_BYTE; + c--; + CLK(1); + } while (c > 0); + + PutbackRMByte(ModRM, (uint8_t)dst); + break; + + case 0x08: + do + { + ROR_BYTE; + c--; + CLK(1); + } while (c > 0); + + PutbackRMByte(ModRM, (uint8_t)dst); + break; + + case 0x10: + do + { + ROLC_BYTE; + c--; + CLK(1); + } while (c > 0); + + PutbackRMByte(ModRM, (uint8_t)dst); + break; + + case 0x18: + do + { + RORC_BYTE; + c--; + CLK(1); + } while (c > 0); + + PutbackRMByte(ModRM, (uint8_t)dst); + break; + + case 0x20: + SHL_BYTE(c); + I.AuxVal = 1; + break; + + case 0x28: + SHR_BYTE(c); + I.AuxVal = 1; + break; + + case 0x30: + break; + + case 0x38: + SHRA_BYTE(c); + break; + } + } +} + +OP(0xd3, i_rotshft_wcl) +{ + uint32_t src, dst; + uint8_t c; + GetModRM; + src = (uint32_t)GetRMWord(ModRM); + dst = src; + c = I.regs.b[CL]; + c &= 0x1f; + CLKM(5, 3); + + if (c) + { + switch (ModRM & 0x38) + { + case 0x00: + do + { + ROL_WORD; + c--; + CLK(1); + } while (c > 0); + + PutbackRMWord(ModRM, (int16_t)dst); + break; + + case 0x08: + do + { + ROR_WORD; + c--; + CLK(1); + } while (c > 0); + + PutbackRMWord(ModRM, (int16_t)dst); + break; + + case 0x10: + do + { + ROLC_WORD; + c--; + CLK(1); + } while (c > 0); + + PutbackRMWord(ModRM, (int16_t)dst); + break; + + case 0x18: + do + { + RORC_WORD; + c--; + CLK(1); + } while (c > 0); - if ( ZF && I.regs.w[CW]) - { - I.ip = (int16_t)(I.ip+disp); - CLK(6); - } - else - { - CLK(3); - } + PutbackRMWord(ModRM, (int16_t)dst); + break; + + case 0x20: + SHL_WORD(c); + I.AuxVal = 1; + break; + + case 0x28: + SHR_WORD(c); + I.AuxVal = 1; + break; + + case 0x30: + break; + + case 0x38: + SHRA_WORD(c); + break; + } + } } -OP( 0xe2, i_loop ) + +OP(0xd4, i_aam) { - int8_t disp = (int8_t)FETCH; - I.regs.w[CW]--; + /*uint32_t mult=FETCH; mult=0;*/ I.regs.b[AH] = I.regs.b[AL] / 10; + I.regs.b[AL] %= 10; + SetSZPF_Word(I.regs.w[AW]); + CLK(17); +} - if (I.regs.w[CW]) - { - I.ip = (int16_t)(I.ip+disp); - CLK(5); - } - else - { - CLK(2); - } +OP(0xd5, i_aad) +{ + /*uint32_t mult=FETCH; mult=0;*/ I.regs.b[AL] = I.regs.b[AH] * 10 + I.regs.b[AL]; + I.regs.b[AH] = 0; + SetSZPF_Byte(I.regs.b[AL]); + CLK(6); } -OP( 0xe3, i_jcxz ) + +OP(0xd6, i_setalc) { - int8_t disp = (int8_t)FETCH; + I.regs.b[AL] = (CF) ? 0xff : 0x00; /* nop at V30MZ? */ + CLK(3); +} - if (I.regs.w[CW] == 0) - { - I.ip = (int16_t)(I.ip+disp); - CLK(4); - } - else - { - CLK(1); - } +OP(0xd7, i_trans) +{ + uint32_t dest = (I.regs.w[BW] + I.regs.b[AL]) & 0xffff; + I.regs.b[AL] = GetMemB(DS, dest); + CLK(5); } -OP( 0xe4, i_inal ) + +OP(0xd8, i_fpo) { - uint8_t port = FETCH; - I.regs.b[AL] = read_port(port); - CLK(6); + /*GetModRM;*/ CLK(3); /* nop at V30MZ? */ } -OP( 0xe5, i_inax ) + +OP(0xe0, i_loopne) { - uint8_t port = FETCH; - I.regs.b[AL] = read_port(port); - I.regs.b[AH] = read_port(port+1); - CLK(6); + int8_t disp = (int8_t)FETCH; + I.regs.w[CW]--; + + if (!ZF && I.regs.w[CW]) + { + I.ip = (int16_t)(I.ip + disp); + CLK(6); + } + else + { + CLK(3); + } } -OP( 0xe6, i_outal ) + +OP(0xe1, i_loope) +{ + int8_t disp = (int8_t)FETCH; + I.regs.w[CW]--; + + if (ZF && I.regs.w[CW]) + { + I.ip = (int16_t)(I.ip + disp); + CLK(6); + } + else + { + CLK(3); + } +} + +OP(0xe2, i_loop) +{ + int8_t disp = (int8_t)FETCH; + I.regs.w[CW]--; + + if (I.regs.w[CW]) + { + I.ip = (int16_t)(I.ip + disp); + CLK(5); + } + else + { + CLK(2); + } +} + +OP(0xe3, i_jcxz) +{ + int8_t disp = (int8_t)FETCH; + + if (I.regs.w[CW] == 0) + { + I.ip = (int16_t)(I.ip + disp); + CLK(4); + } + else + { + CLK(1); + } +} + +OP(0xe4, i_inal) { - uint8_t port = FETCH; - write_port(port, I.regs.b[AL]); - CLK(6); + uint8_t port = FETCH; + I.regs.b[AL] = read_port(port); + CLK(6); } -OP( 0xe7, i_outax ) + +OP(0xe5, i_inax) { - uint8_t port = FETCH; - write_port(port, I.regs.b[AL]); - write_port(port+1, I.regs.b[AH]); - CLK(6); + uint8_t port = FETCH; + I.regs.b[AL] = read_port(port); + I.regs.b[AH] = read_port(port + 1); + CLK(6); } -OP( 0xe8, i_call_d16 ) +OP(0xe6, i_outal) { - uint32_t tmp; - FETCHWORD(tmp); - PUSH(I.ip); - I.ip = (int16_t)(I.ip+(int16_t)tmp); - CLK(5); + uint8_t port = FETCH; + write_port(port, I.regs.b[AL]); + CLK(6); } -OP( 0xe9, i_jmp_d16 ) + +OP(0xe7, i_outax) { - uint32_t tmp; - FETCHWORD(tmp); - //printf("@ %04X -> %04X\n", I.ip-3, tmp); - I.ip = (int16_t)(I.ip+(int16_t)tmp); - CLK(4); + uint8_t port = FETCH; + write_port(port, I.regs.b[AL]); + write_port(port + 1, I.regs.b[AH]); + CLK(6); } -OP( 0xea, i_jmp_far ) -{ - uint32_t tmp,tmp1; - FETCHWORD(tmp); - FETCHWORD(tmp1); - I.sregs[CS] = (int16_t)tmp1; - I.ip = (int16_t)tmp; - CLK(7); -} -OP( 0xeb, i_jmp_d8 ) + +OP(0xe8, i_call_d16) { - int tmp = (int)((int8_t)FETCH); - CLK(4); + uint32_t tmp; + FETCHWORD(tmp); + PUSH(I.ip); + I.ip = (int16_t)(I.ip + (int16_t)tmp); + CLK(5); +} - if (tmp==-2 && no_interrupt==0 && nec_ICount>0) - { - nec_ICount%=12; /* cycle skip */ - } - - I.ip = (int16_t)(I.ip+tmp); -} -OP( 0xec, i_inaldx ) +OP(0xe9, i_jmp_d16) { - I.regs.b[AL] = read_port(I.regs.w[DW]); - CLK(6); + uint32_t tmp; + FETCHWORD(tmp); + //printf("@ %04X -> %04X\n", I.ip-3, tmp); + I.ip = (int16_t)(I.ip + (int16_t)tmp); + CLK(4); } -OP( 0xed, i_inaxdx ) + +OP(0xea, i_jmp_far) { - uint32_t port = I.regs.w[DW]; - I.regs.b[AL] = read_port(port); - I.regs.b[AH] = read_port(port+1); - CLK(6); + uint32_t tmp, tmp1; + FETCHWORD(tmp); + FETCHWORD(tmp1); + I.sregs[CS] = (int16_t)tmp1; + I.ip = (int16_t)tmp; + CLK(7); } -OP( 0xee, i_outdxal ) -{ - write_port(I.regs.w[DW], I.regs.b[AL]); - CLK(6); + +OP(0xeb, i_jmp_d8) +{ + int tmp = (int)((int8_t)FETCH); + CLK(4); + + if (tmp == -2 && no_interrupt == 0 && nec_ICount > 0) + { + nec_ICount %= 12; /* cycle skip */ + } + + I.ip = (int16_t)(I.ip + tmp); } -OP( 0xef, i_outdxax ) + +OP(0xec, i_inaldx) { - uint32_t port = I.regs.w[DW]; - write_port(port, I.regs.b[AL]); - write_port(port+1, I.regs.b[AH]); - CLK(6); + I.regs.b[AL] = read_port(I.regs.w[DW]); + CLK(6); } -OP( 0xf0, i_lock ) -{ - no_interrupt=1; - CLK(1); +OP(0xed, i_inaxdx) +{ + uint32_t port = I.regs.w[DW]; + I.regs.b[AL] = read_port(port); + I.regs.b[AH] = read_port(port + 1); + CLK(6); } -#define THROUGH \ - if(nec_ICount<0){ \ - if(seg_prefix) \ - I.ip-=(uint16_t)3; \ - else \ - I.ip-=(uint16_t)2; \ - break;} -OP( 0xf2, i_repne ) +OP(0xee, i_outdxal) { - uint32_t next = FETCHOP; - uint16_t c = I.regs.w[CW]; + write_port(I.regs.w[DW], I.regs.b[AL]); + CLK(6); +} + +OP(0xef, i_outdxax) +{ + uint32_t port = I.regs.w[DW]; + write_port(port, I.regs.b[AL]); + write_port(port + 1, I.regs.b[AH]); + CLK(6); +} + +OP(0xf0, i_lock) +{ + no_interrupt = 1; + CLK(1); +} + +#define THROUGH \ + if(nec_ICount<0){ \ + if(seg_prefix) \ + I.ip-=(uint16_t)3; \ + else \ + I.ip-=(uint16_t)2; \ + break;} + +OP(0xf2, i_repne) +{ + uint32_t next = FETCHOP; + uint16_t c = I.regs.w[CW]; + + switch (next) /* Segments */ + { + case 0x26: + seg_prefix = TRUE; + prefix_base = I.sregs[ES] << 4; + next = FETCHOP; + CLK(2); + break; + + case 0x2e: + seg_prefix = TRUE; + prefix_base = I.sregs[CS] << 4; + next = FETCHOP; + CLK(2); + break; + + case 0x36: + seg_prefix = TRUE; + prefix_base = I.sregs[SS] << 4; + next = FETCHOP; + CLK(2); + break; + + case 0x3e: + seg_prefix = TRUE; + prefix_base = I.sregs[DS] << 4; + next = FETCHOP; + CLK(2); + break; + } + + switch (next) + { + case 0x6c: + CLK(2); + + if (c) + { + do + { + i_insb(); + c--; + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0x6d: + CLK(2); + + if (c) + { + do + { + i_insw(); + c--; + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0x6e: + CLK(2); + + if (c) + { + do + { + i_outsb(); + c--; + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0x6f: + CLK(2); + + if (c) + { + do + { + i_outsw(); + c--; + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0xa4: + CLK(2); + + if (c) + { + do + { + i_movsb(); + c--; + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0xa5: + CLK(2); + + if (c) + { + do + { + i_movsw(); + c--; + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0xa6: + CLK(5); + + if (c) + { + do + { + THROUGH; + i_cmpsb(); + c--; + CLK(3); + } while (c > 0 && ZF == 0); + } + + I.regs.w[CW] = c; + break; + + case 0xa7: + CLK(5); + + if (c) + { + do + { + THROUGH; + i_cmpsw(); + c--; + CLK(3); + } while (c > 0 && ZF == 0); + } + + I.regs.w[CW] = c; + break; + + case 0xaa: + CLK(2); + + if (c) + { + do + { + i_stosb(); + c--; + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0xab: + CLK(2); + + if (c) + { + do + { + i_stosw(); + c--; + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0xac: + CLK(2); + + if (c) + { + do + { + i_lodsb(); + c--; + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0xad: + CLK(2); + + if (c) + { + do + { + i_lodsw(); + c--; + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0xae: + CLK(5); + + if (c) + { + do + { + THROUGH; + i_scasb(); + c--; + CLK(5); + } while (c > 0 && ZF == 0); + } + + I.regs.w[CW] = c; + break; + + case 0xaf: + CLK(5); + + if (c) + { + do + { + THROUGH; + i_scasw(); + c--; + CLK(5); + } while (c > 0 && ZF == 0); + } + + I.regs.w[CW] = c; + break; + + default: + nec_instruction[next](); + } + + seg_prefix = FALSE; +} + +OP(0xf3, i_repe) +{ + uint32_t next = FETCHOP; + uint16_t c = I.regs.w[CW]; + + switch (next) /* Segments */ + { + case 0x26: + seg_prefix = TRUE; + prefix_base = I.sregs[ES] << 4; + next = FETCHOP; + CLK(2); + break; + + case 0x2e: + seg_prefix = TRUE; + prefix_base = I.sregs[CS] << 4; + next = FETCHOP; + CLK(2); + break; + + case 0x36: + seg_prefix = TRUE; + prefix_base = I.sregs[SS] << 4; + next = FETCHOP; + CLK(2); + break; + + case 0x3e: + seg_prefix = TRUE; + prefix_base = I.sregs[DS] << 4; + next = FETCHOP; + CLK(2); + break; + } + + switch (next) + { + case 0x6c: + CLK(5); + + if (c) + { + do + { + THROUGH; + i_insb(); + c--; + CLK(0); + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0x6d: + CLK(5); + + if (c) + { + do + { + THROUGH; + i_insw(); + c--; + CLK(0); + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0x6e: + CLK(5); + + if (c) + { + do + { + THROUGH; + i_outsb(); + c--; + CLK(-1); + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0x6f: + CLK(5); + + if (c) + { + do + { + THROUGH; + i_outsw(); + c--; + CLK(-1); + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0xa4: + CLK(5); + + if (c) + { + do + { + THROUGH; + i_movsb(); + c--; + CLK(2); + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0xa5: + CLK(5); + + if (c) + { + do + { + THROUGH; + i_movsw(); + c--; + CLK(2); + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0xa6: + CLK(5); + + if (c) + { + do + { + THROUGH; + i_cmpsb(); + c--; + CLK(4); + } while (c > 0 && ZF == 1); + } + + I.regs.w[CW] = c; + break; + + case 0xa7: + CLK(5); + + if (c) + { + do + { + THROUGH; + i_cmpsw(); + c--; + CLK(4); + } while (c > 0 && ZF == 1); + } + + I.regs.w[CW] = c; + break; + + case 0xaa: + CLK(5); + + if (c) + { + do + { + THROUGH; + i_stosb(); + c--; + CLK(3); + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0xab: + CLK(5); + + if (c) + { + do + { + THROUGH; + i_stosw(); + c--; + CLK(3); + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0xac: + CLK(5); + + if (c) + { + do + { + THROUGH; + i_lodsb(); + c--; + CLK(3); + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0xad: + CLK(5); + + if (c) + { + do + { + THROUGH; + i_lodsw(); + c--; + CLK(3); + } while (c > 0); + } + + I.regs.w[CW] = c; + break; + + case 0xae: + CLK(5); + + if (c) + { + do + { + THROUGH; + i_scasb(); + c--; + CLK(4); + } while (c > 0 && ZF == 1); + } + + I.regs.w[CW] = c; + break; + + case 0xaf: + CLK(5); + + if (c) + { + do + { + THROUGH; + i_scasw(); + c--; + CLK(4); + } while (c > 0 && ZF == 1); + } + + I.regs.w[CW] = c; + break; + + default: + nec_instruction[next](); + } + + seg_prefix = FALSE; +} + +OP(0xf4, i_hlt) +{ + //printf("Halted @ %04X:%04X...\n", I.sregs[CS], I.ip - 1); + nec_ICount = 0; +} + + +OP(0xf5, i_cmc) +{ + I.CarryVal = !CF; + CLK(4); +} + +OP(0xf6, i_f6pre) +{ + uint32_t tmp; + uint32_t uresult, uresult2; + int32_t result, result2; + GetModRM; + tmp = GetRMByte(ModRM); + + switch (ModRM & 0x38) + { + case 0x00: + tmp &= FETCH; + I.CarryVal = I.OverVal = I.AuxVal = 0; + SetSZPF_Byte(tmp); + CLKM(2, 1); + break; /* TEST */ + + case 0x08: + break; + + case 0x10: + PutbackRMByte(ModRM, ~tmp); + CLKM(3, 1); + break; /* NOT */ + + case 0x18: + I.CarryVal = (tmp != 0); + tmp = (~tmp) + 1; + SetSZPF_Byte(tmp); + PutbackRMByte(ModRM, tmp & 0xff); + CLKM(3, 1); + break; /* NEG */ + + case 0x20: + uresult = I.regs.b[AL] * tmp; + I.regs.w[AW] = (int16_t)uresult; + I.CarryVal = I.OverVal = (I.regs.b[AH] != 0); + CLKM(4, 3); + break; /* MULU */ + + case 0x28: + result = (int16_t)((int8_t)I.regs.b[AL]) * (int16_t)((int8_t)tmp); + I.regs.w[AW] = (int16_t)result; + I.CarryVal = I.OverVal = (I.regs.b[AH] != 0); + CLKM(4, 3); + break; /* MUL */ + + case 0x30: + if (tmp) + { + DIVUB; + } + else + { + nec_interrupt(0, 0); + } + + CLKM(16, 15); + break; + + case 0x38: + if (tmp) + { + DIVB; + } + else + { + nec_interrupt(0, 0); + } + + CLKM(18, 17); + break; + } +} + +OP(0xf7, i_f7pre) +{ + uint32_t tmp, tmp2; + uint32_t uresult, uresult2; + int32_t result, result2; + GetModRM; + tmp = GetRMWord(ModRM); + + switch (ModRM & 0x38) + { + case 0x00: + FETCHWORD(tmp2); + tmp &= tmp2; + I.CarryVal = I.OverVal = I.AuxVal = 0; + SetSZPF_Word(tmp); + CLKM(2, 1); + break; /* TEST */ + + case 0x08: + break; + + case 0x10: + PutbackRMWord(ModRM, ~tmp); + CLKM(3, 1); + break; /* NOT */ + + case 0x18: + I.CarryVal = (tmp != 0); + tmp = (~tmp) + 1; + SetSZPF_Word(tmp); + PutbackRMWord(ModRM, tmp & 0xffff); + CLKM(3, 1); + break; /* NEG */ + + case 0x20: + uresult = I.regs.w[AW] * tmp; + I.regs.w[AW] = uresult & 0xffff; + I.regs.w[DW] = ((uint32_t)uresult) >> 16; + I.CarryVal = I.OverVal = (I.regs.w[DW] != 0); + CLKM(4, 3); + break; /* MULU */ + + case 0x28: + result = (int32_t)((int16_t)I.regs.w[AW]) * (int32_t)((int16_t)tmp); + I.regs.w[AW] = result & 0xffff; + I.regs.w[DW] = result >> 16; + I.CarryVal = I.OverVal = (I.regs.w[DW] != 0); + CLKM(4, 3); + break; /* MUL */ + + case 0x30: + if (tmp) + { + DIVUW; + } + else + { + nec_interrupt(0, 0); + } + + CLKM(24, 23); + break; + + case 0x38: + if (tmp) + { + DIVW; + } + else + { + nec_interrupt(0, 0); + } + + CLKM(25, 24); + break; + } +} + +OP(0xf8, i_clc) +{ + I.CarryVal = 0; + CLK(4); +} + +OP(0xf9, i_stc) +{ + I.CarryVal = 1; + CLK(4); +} + +OP(0xfa, i_di) +{ + SetIF(0); + CLK(4); +} + +OP(0xfb, i_ei) +{ + /* STI */ + SetIF(1); + CLK(4); +} + +OP(0xfc, i_cld) +{ + SetDF(0); + CLK(4); +} + +OP(0xfd, i_std) +{ + SetDF(1); + CLK(4); +} + +OP(0xfe, i_fepre) +{ + uint32_t tmp, tmp1; + GetModRM; + tmp = GetRMByte(ModRM); + + switch (ModRM & 0x38) + { + case 0x00: + tmp1 = tmp + 1; + I.OverVal = (tmp == 0x7f); + SetAF(tmp1, tmp, 1); + SetSZPF_Byte(tmp1); + PutbackRMByte(ModRM, (uint8_t)tmp1); + CLKM(3, 1); + break; /* INC */ + + case 0x08: + tmp1 = tmp - 1; + I.OverVal = (tmp == 0x80); + SetAF(tmp1, tmp, 1); + SetSZPF_Byte(tmp1); + PutbackRMByte(ModRM, (uint8_t)tmp1); + CLKM(3, 1); + break; /* DEC */ + } +} + +OP(0xff, i_ffpre) +{ + uint32_t tmp, tmp1; + GetModRM; + tmp = GetRMWord(ModRM); + + switch (ModRM & 0x38) + { + case 0x00: + tmp1 = tmp + 1; + I.OverVal = (tmp == 0x7fff); + SetAF(tmp1, tmp, 1); + SetSZPF_Word(tmp1); + PutbackRMWord(ModRM, (int16_t)tmp1); + CLKM(3, 1); + break; /* INC */ + + case 0x08: + tmp1 = tmp - 1; + I.OverVal = (tmp == 0x8000); + SetAF(tmp1, tmp, 1); + SetSZPF_Word(tmp1); + PutbackRMWord(ModRM, (int16_t)tmp1); + CLKM(3, 1); + break; /* DEC */ + + case 0x10: + PUSH(I.ip); + I.ip = (int16_t)tmp; + CLKM(6, 5); + break; /* CALL */ + + case 0x18: + tmp1 = I.sregs[CS]; + I.sregs[CS] = GetnextRMWord; + PUSH(tmp1); + PUSH(I.ip); + I.ip = tmp; + CLKM(12, 1); + break; /* CALL FAR */ + + case 0x20: + //printf("@ %04X -> %04X\n", I.ip, tmp); + I.ip = tmp; + CLKM(5, 4); + break; /* JMP */ - switch(next) /* Segments */ - { - case 0x26: - seg_prefix=TRUE; - prefix_base=I.sregs[ES]<<4; - next = FETCHOP; - CLK(2); - break; - - case 0x2e: - seg_prefix=TRUE; - prefix_base=I.sregs[CS]<<4; - next = FETCHOP; - CLK(2); - break; - - case 0x36: - seg_prefix=TRUE; - prefix_base=I.sregs[SS]<<4; - next = FETCHOP; - CLK(2); - break; - - case 0x3e: - seg_prefix=TRUE; - prefix_base=I.sregs[DS]<<4; - next = FETCHOP; - CLK(2); - break; - } - - switch(next) - { - case 0x6c: - CLK(2); - - if (c) do - { - i_insb(); - c--; - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0x6d: - CLK(2); - - if (c) do - { - i_insw(); - c--; - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0x6e: - CLK(2); - - if (c) do - { - i_outsb(); - c--; - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0x6f: - CLK(2); - - if (c) do - { - i_outsw(); - c--; - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0xa4: - CLK(2); - - if (c) do - { - i_movsb(); - c--; - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0xa5: - CLK(2); - - if (c) do - { - i_movsw(); - c--; - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0xa6: - CLK(5); - - if (c) do - { - THROUGH; - i_cmpsb(); - c--; - CLK(3); - } - while (c>0 && ZF==0); - - I.regs.w[CW]=c; - break; - - case 0xa7: - CLK(5); - - if (c) do - { - THROUGH; - i_cmpsw(); - c--; - CLK(3); - } - while (c>0 && ZF==0); - - I.regs.w[CW]=c; - break; - - case 0xaa: - CLK(2); - - if (c) do - { - i_stosb(); - c--; - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0xab: - CLK(2); - - if (c) do - { - i_stosw(); - c--; - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0xac: - CLK(2); - - if (c) do - { - i_lodsb(); - c--; - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0xad: - CLK(2); - - if (c) do - { - i_lodsw(); - c--; - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0xae: - CLK(5); - - if (c) do - { - THROUGH; - i_scasb(); - c--; - CLK(5); - } - while (c>0 && ZF==0); - - I.regs.w[CW]=c; - break; - - case 0xaf: - CLK(5); - - if (c) do - { - THROUGH; - i_scasw(); - c--; - CLK(5); - } - while (c>0 && ZF==0); - - I.regs.w[CW]=c; - break; - - default: - nec_instruction[next](); - } - - seg_prefix=FALSE; -} -OP( 0xf3, i_repe ) -{ - uint32_t next = FETCHOP; - uint16_t c = I.regs.w[CW]; - - switch(next) /* Segments */ - { - case 0x26: - seg_prefix=TRUE; - prefix_base=I.sregs[ES]<<4; - next = FETCHOP; - CLK(2); - break; - - case 0x2e: - seg_prefix=TRUE; - prefix_base=I.sregs[CS]<<4; - next = FETCHOP; - CLK(2); - break; - - case 0x36: - seg_prefix=TRUE; - prefix_base=I.sregs[SS]<<4; - next = FETCHOP; - CLK(2); - break; - - case 0x3e: - seg_prefix=TRUE; - prefix_base=I.sregs[DS]<<4; - next = FETCHOP; - CLK(2); - break; - } - - switch(next) - { - case 0x6c: - CLK(5); - - if (c) do - { - THROUGH; - i_insb(); - c--; - CLK( 0); - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0x6d: - CLK(5); - - if (c) do - { - THROUGH; - i_insw(); - c--; - CLK( 0); - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0x6e: - CLK(5); - - if (c) do - { - THROUGH; - i_outsb(); - c--; - CLK(-1); - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0x6f: - CLK(5); - - if (c) do - { - THROUGH; - i_outsw(); - c--; - CLK(-1); - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0xa4: - CLK(5); - - if (c) do - { - THROUGH; - i_movsb(); - c--; - CLK( 2); - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0xa5: - CLK(5); - - if (c) do - { - THROUGH; - i_movsw(); - c--; - CLK( 2); - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0xa6: - CLK(5); - - if (c) do - { - THROUGH; - i_cmpsb(); - c--; - CLK( 4); - } - while (c>0 && ZF==1); - - I.regs.w[CW]=c; - break; - - case 0xa7: - CLK(5); - - if (c) do - { - THROUGH; - i_cmpsw(); - c--; - CLK( 4); - } - while (c>0 && ZF==1); - - I.regs.w[CW]=c; - break; - - case 0xaa: - CLK(5); - - if (c) do - { - THROUGH; - i_stosb(); - c--; - CLK( 3); - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0xab: - CLK(5); - - if (c) do - { - THROUGH; - i_stosw(); - c--; - CLK( 3); - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0xac: - CLK(5); - - if (c) do - { - THROUGH; - i_lodsb(); - c--; - CLK( 3); - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0xad: - CLK(5); - - if (c) do - { - THROUGH; - i_lodsw(); - c--; - CLK( 3); - } - while (c>0); - - I.regs.w[CW]=c; - break; - - case 0xae: - CLK(5); - - if (c) do - { - THROUGH; - i_scasb(); - c--; - CLK( 4); - } - while (c>0 && ZF==1); - - I.regs.w[CW]=c; - break; - - case 0xaf: - CLK(5); - - if (c) do - { - THROUGH; - i_scasw(); - c--; - CLK( 4); - } - while (c>0 && ZF==1); - - I.regs.w[CW]=c; - break; - - default: - nec_instruction[next](); - } - - seg_prefix=FALSE; -} -OP( 0xf4, i_hlt ) -{ - //printf("Halted @ %04X:%04X...\n", I.sregs[CS], I.ip - 1); - nec_ICount=0; -} - - - - - -OP( 0xf5, i_cmc ) -{ - I.CarryVal = !CF; - CLK(4); -} -OP( 0xf6, i_f6pre ) -{ - uint32_t tmp; - uint32_t uresult,uresult2; - int32_t result,result2; - GetModRM; - tmp = GetRMByte(ModRM); - - switch (ModRM & 0x38) - { - case 0x00: - tmp &= FETCH; - I.CarryVal = I.OverVal = I.AuxVal=0; - SetSZPF_Byte(tmp); - CLKM(2,1); - break; /* TEST */ - - case 0x08: - break; - - case 0x10: - PutbackRMByte(ModRM,~tmp); - CLKM(3,1); - break; /* NOT */ - - case 0x18: - I.CarryVal=(tmp!=0); - tmp=(~tmp)+1; - SetSZPF_Byte(tmp); - PutbackRMByte(ModRM,tmp&0xff); - CLKM(3,1); - break; /* NEG */ - - case 0x20: - uresult = I.regs.b[AL]*tmp; - I.regs.w[AW]=(int16_t)uresult; - I.CarryVal=I.OverVal=(I.regs.b[AH]!=0); - CLKM(4,3); - break; /* MULU */ - - case 0x28: - result = (int16_t)((int8_t)I.regs.b[AL])*(int16_t)((int8_t)tmp); - I.regs.w[AW]=(int16_t)result; - I.CarryVal=I.OverVal=(I.regs.b[AH]!=0); - CLKM(4,3); - break; /* MUL */ - - case 0x30: - if (tmp) - { - DIVUB; - } - else - { - nec_interrupt(0,0); - } - - CLKM(16,15); - break; - - case 0x38: - if (tmp) - { - DIVB; - } - else - { - nec_interrupt(0,0); - } - - CLKM(18,17); - break; - } -} - -OP( 0xf7, i_f7pre ) -{ - uint32_t tmp,tmp2; - uint32_t uresult,uresult2; - int32_t result,result2; - GetModRM; - tmp = GetRMWord(ModRM); - - switch (ModRM & 0x38) - { - case 0x00: - FETCHWORD(tmp2); - tmp &= tmp2; - I.CarryVal = I.OverVal = I.AuxVal=0; - SetSZPF_Word(tmp); - CLKM(2,1); - break; /* TEST */ - - case 0x08: - break; - - case 0x10: - PutbackRMWord(ModRM,~tmp); - CLKM(3,1); - break; /* NOT */ - - case 0x18: - I.CarryVal=(tmp!=0); - tmp=(~tmp)+1; - SetSZPF_Word(tmp); - PutbackRMWord(ModRM,tmp&0xffff); - CLKM(3,1); - break; /* NEG */ - - case 0x20: - uresult = I.regs.w[AW]*tmp; - I.regs.w[AW]=uresult&0xffff; - I.regs.w[DW]=((uint32_t)uresult)>>16; - I.CarryVal=I.OverVal=(I.regs.w[DW]!=0); - CLKM(4,3); - break; /* MULU */ - - case 0x28: - result = (int32_t)((int16_t)I.regs.w[AW])*(int32_t)((int16_t)tmp); - I.regs.w[AW]=result&0xffff; - I.regs.w[DW]=result>>16; - I.CarryVal=I.OverVal=(I.regs.w[DW]!=0); - CLKM(4,3); - break; /* MUL */ - - case 0x30: - if (tmp) - { - DIVUW; - } - else - { - nec_interrupt(0,0); - } - - CLKM(24,23); - break; - - case 0x38: - if (tmp) - { - DIVW; - } - else - { - nec_interrupt(0,0); - } - - CLKM(25,24); - break; - } -} - -OP( 0xf8, i_clc ) -{ - I.CarryVal = 0; - CLK(4); -} -OP( 0xf9, i_stc ) -{ - I.CarryVal = 1; - CLK(4); -} -OP( 0xfa, i_di ) -{ - SetIF(0); - CLK(4); -} -OP( 0xfb, i_ei ) -{ - /* STI */ - SetIF(1); - CLK(4); -} -OP( 0xfc, i_cld ) -{ - SetDF(0); - CLK(4); -} -OP( 0xfd, i_std ) -{ - SetDF(1); - CLK(4); -} -OP( 0xfe, i_fepre ) -{ - uint32_t tmp, tmp1; - GetModRM; - tmp=GetRMByte(ModRM); - - switch(ModRM & 0x38) - { - case 0x00: - tmp1 = tmp+1; - I.OverVal = (tmp==0x7f); - SetAF(tmp1,tmp,1); - SetSZPF_Byte(tmp1); - PutbackRMByte(ModRM,(uint8_t)tmp1); - CLKM(3,1); - break; /* INC */ - - case 0x08: - tmp1 = tmp-1; - I.OverVal = (tmp==0x80); - SetAF(tmp1,tmp,1); - SetSZPF_Byte(tmp1); - PutbackRMByte(ModRM,(uint8_t)tmp1); - CLKM(3,1); - break; /* DEC */ - } -} -OP( 0xff, i_ffpre ) -{ - uint32_t tmp, tmp1; - GetModRM; - tmp=GetRMWord(ModRM); - - switch(ModRM & 0x38) - { - case 0x00: - tmp1 = tmp+1; - I.OverVal = (tmp==0x7fff); - SetAF(tmp1,tmp,1); - SetSZPF_Word(tmp1); - PutbackRMWord(ModRM,(int16_t)tmp1); - CLKM(3,1); - break; /* INC */ - - case 0x08: - tmp1 = tmp-1; - I.OverVal = (tmp==0x8000); - SetAF(tmp1,tmp,1); - SetSZPF_Word(tmp1); - PutbackRMWord(ModRM,(int16_t)tmp1); - CLKM(3,1); - break; /* DEC */ - - case 0x10: - PUSH(I.ip); - I.ip = (int16_t)tmp; - CLKM(6,5); - break; /* CALL */ - - case 0x18: - tmp1 = I.sregs[CS]; - I.sregs[CS] = GetnextRMWord; - PUSH(tmp1); - PUSH(I.ip); - I.ip = tmp; - CLKM(12,1); - break; /* CALL FAR */ - - case 0x20: - //printf("@ %04X -> %04X\n", I.ip, tmp); - I.ip = tmp; - CLKM(5,4); - break; /* JMP */ - - case 0x28: - I.ip = tmp; - I.sregs[CS] = GetnextRMWord; - - //printf("New CS: %04X, New IP: %04X\n", I.sregs[CS], I.ip); - - CLKM(10,1); - break; /* JMP FAR */ - - case 0x30: - PUSH(tmp); - CLKM(2,1); - break; - - default: - ; - } + case 0x28: + I.ip = tmp; + I.sregs[CS] = GetnextRMWord; + + //printf("New CS: %04X, New IP: %04X\n", I.sregs[CS], I.ip); + + CLKM(10, 1); + break; /* JMP FAR */ + + case 0x30: + PUSH(tmp); + CLKM(2, 1); + break; + + default:; + } } static void i_invalid(void) { - CLK(10); + CLK(10); } /*****************************************************************************/ @@ -3765,175 +4081,177 @@ static void i_invalid(void) uint32_t nec_get_reg(int regnum) { - switch( regnum ) - { - case NEC_IP: - return I.ip; + switch (regnum) + { + case NEC_IP: + return I.ip; - case NEC_SP: - return I.regs.w[SP]; + case NEC_SP: + return I.regs.w[SP]; - case NEC_FLAGS: - return CompressFlags(); + case NEC_FLAGS: + return CompressFlags(); - case NEC_AW: - return I.regs.w[AW]; + case NEC_AW: + return I.regs.w[AW]; - case NEC_CW: - return I.regs.w[CW]; + case NEC_CW: + return I.regs.w[CW]; - case NEC_DW: - return I.regs.w[DW]; + case NEC_DW: + return I.regs.w[DW]; - case NEC_BW: - return I.regs.w[BW]; + case NEC_BW: + return I.regs.w[BW]; - case NEC_BP: - return I.regs.w[BP]; + case NEC_BP: + return I.regs.w[BP]; - case NEC_IX: - return I.regs.w[IX]; + case NEC_IX: + return I.regs.w[IX]; - case NEC_IY: - return I.regs.w[IY]; + case NEC_IY: + return I.regs.w[IY]; - case NEC_ES: - return I.sregs[ES]; + case NEC_ES: + return I.sregs[ES]; - case NEC_CS: - return I.sregs[CS]; + case NEC_CS: + return I.sregs[CS]; - case NEC_SS: - return I.sregs[SS]; + case NEC_SS: + return I.sregs[SS]; - case NEC_DS: - return I.sregs[DS]; + case NEC_DS: + return I.sregs[DS]; - case NEC_VECTOR: - return I.int_vector; + case NEC_VECTOR: + return I.int_vector; - case NEC_PENDING: - return I.pending_irq; + case NEC_PENDING: + return I.pending_irq; - case NEC_NMI_STATE: - return I.nmi_state; + case NEC_NMI_STATE: + return I.nmi_state; - case NEC_IRQ_STATE: - return I.irq_state; - } + case NEC_IRQ_STATE: + return I.irq_state; + } - return 0; + return 0; } void nec_set_reg(int regnum, uint32_t val) { - switch( regnum ) - { - case NEC_IP: - I.ip = val; - break; + switch (regnum) + { + case NEC_IP: + I.ip = val; + break; - case NEC_SP: - I.regs.w[SP] = val; - break; + case NEC_SP: + I.regs.w[SP] = val; + break; - case NEC_FLAGS: - ExpandFlags(val); - break; + case NEC_FLAGS: + ExpandFlags(val); + break; - case NEC_AW: - I.regs.w[AW] = val; - break; + case NEC_AW: + I.regs.w[AW] = val; + break; - case NEC_CW: - I.regs.w[CW] = val; - break; + case NEC_CW: + I.regs.w[CW] = val; + break; - case NEC_DW: - I.regs.w[DW] = val; - break; + case NEC_DW: + I.regs.w[DW] = val; + break; - case NEC_BW: - I.regs.w[BW] = val; - break; + case NEC_BW: + I.regs.w[BW] = val; + break; - case NEC_BP: - I.regs.w[BP] = val; - break; + case NEC_BP: + I.regs.w[BP] = val; + break; - case NEC_IX: - I.regs.w[IX] = val; - break; + case NEC_IX: + I.regs.w[IX] = val; + break; - case NEC_IY: - I.regs.w[IY] = val; - break; + case NEC_IY: + I.regs.w[IY] = val; + break; - case NEC_ES: - I.sregs[ES] = val; - break; + case NEC_ES: + I.sregs[ES] = val; + break; - case NEC_CS: - I.sregs[CS] = val; - break; + case NEC_CS: + I.sregs[CS] = val; + break; - case NEC_SS: - I.sregs[SS] = val; - break; + case NEC_SS: + I.sregs[SS] = val; + break; - case NEC_DS: - I.sregs[DS] = val; - break; + case NEC_DS: + I.sregs[DS] = val; + break; - case NEC_VECTOR: - I.int_vector = val; - break; - } + case NEC_VECTOR: + I.int_vector = val; + break; + } } -const char *instructionsName[256] = -{ - "ADD ", "ADD ", "ADD ", "ADD ", "ADD ", "ADD ", "PUSH", "POP ", "OR ", "OR ", "OR ", "OR ", "OR ", "OR ", "PUSH", "----", - "ADC ", "ADC ", "ADC ", "ADC ", "ADC ", "ADC ", "PUSH", "POP ", "SBB ", "SBB ", "SBB ", "SBB ", "SBB ", "SBB ", "PUSH", "POP ", - "AND ", "AND ", "AND ", "AND ", "AND ", "AND ", "ES: ", "DAA ", "SUB ", "SUB ", "SUB ", "SUB ", "SUB ", "SUB ", "CS: ", "DAS ", - "XOR ", "XOR ", "XOR ", "XOR ", "XOR ", "XOR ", "SS: ", "AAA ", "CMP ", "CMP ", "CMP ", "CMP ", "CMP ", "CMP ", "ES: ", "AAS ", - "INC ", "INC ", "INC ", "INC ", "INC ", "INC ", "INC ", "DEC ", "DEC ", "DEC ", "DEC ", "DEC ", "DEC ", "DEC ", "DEC ", "DEC ", - "PUSH", "PUSH", "PUSH", "PUSH", "PUSH", "PUSH", "PUSH", "PUSH", "POP ", "POP ", "POP ", "POP ", "POP ", "POP ", "POP ", "POP ", - "PUSA", "POPA", "BOND", "----", "----", "----", "----", "----", "PUSH", "IMUL", "PUSH", "IMUL", "INS ", "INS ", "OUTS", "OUTS", - "JO ", "JNO ", "JB ", "JNB ", "JZ ", "JNZ ", "JBE ", "JA ", "JS ", "JNS ", "JPE ", "JPO ", "JL ", "JGE ", "JLE ", "JG ", - "GPR1", "GPR1", "GPR1", "GPR1", "TEST", "TEST", "XCHG", "XCHG", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "LEA ", "MOV ", "POP ", - "NOP ", "XCHA", "XCHA", "XCHA", "XCHA", "XCHA", "XCHA", "XCHA", "CBW ", "CWD ", "CALL", "WAIT", "PSHF", "POPF", "SAHF", "LAHF", - "MOV ", "MOV ", "MOV ", "MOV ", "MOVS", "MOVS", "CMPS", "CMPS", "TEST", "TEST", "STOS", "STOS", "LODS", "LODS", "SCAS", "SCAS", - "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", - "GRP2", "GRP2", "RETN", "RETN", "LES ", "LDS ", "MOV ", "MOV ", "ENTR", "LEAV", "RETF", "RETF", "INT3", "INT ", "INTO", "IRET", - "GRP2", "GRP2", "GRP2", "GRP2", "AAM ", "AAD ", "----", "XLAT", "----", "----", "----", "----", "----", "----", "----", "----", - "LPNZ", "LPZ ", "LOOP", "JCXZ", "IN ", "IN ", "OUT ", "OUT ", "CALL", "JMP ", "JMP ", "JMP ", "IN ", "IN ", "OUT ", "OUT ", - "LOCK", "----", "RPNZ", "REP ", "HLT ", "CMC ", "GP3A", "GP3B", "CLC ", "STC ", "CLI ", "STI ", "CLD ", "STD ", "GPR4", "GRP5" +const char *instructionsName[256] = { + "ADD ", "ADD ", "ADD ", "ADD ", "ADD ", "ADD ", "PUSH", "POP ", "OR ", "OR ", "OR ", "OR ", "OR ", "OR ", + "PUSH", "----", "ADC ", "ADC ", "ADC ", "ADC ", "ADC ", "ADC ", "PUSH", "POP ", "SBB ", "SBB ", "SBB ", "SBB ", + "SBB ", "SBB ", "PUSH", "POP ", "AND ", "AND ", "AND ", "AND ", "AND ", "AND ", "ES: ", "DAA ", "SUB ", "SUB ", + "SUB ", "SUB ", "SUB ", "SUB ", "CS: ", "DAS ", "XOR ", "XOR ", "XOR ", "XOR ", "XOR ", "XOR ", "SS: ", "AAA ", + "CMP ", "CMP ", "CMP ", "CMP ", "CMP ", "CMP ", "ES: ", "AAS ", "INC ", "INC ", "INC ", "INC ", "INC ", "INC ", + "INC ", "DEC ", "DEC ", "DEC ", "DEC ", "DEC ", "DEC ", "DEC ", "DEC ", "DEC ", "PUSH", "PUSH", "PUSH", "PUSH", + "PUSH", "PUSH", "PUSH", "PUSH", "POP ", "POP ", "POP ", "POP ", "POP ", "POP ", "POP ", "POP ", "PUSA", "POPA", + "BOND", "----", "----", "----", "----", "----", "PUSH", "IMUL", "PUSH", "IMUL", "INS ", "INS ", "OUTS", "OUTS", + "JO ", "JNO ", "JB ", "JNB ", "JZ ", "JNZ ", "JBE ", "JA ", "JS ", "JNS ", "JPE ", "JPO ", "JL ", "JGE ", + "JLE ", "JG ", "GPR1", "GPR1", "GPR1", "GPR1", "TEST", "TEST", "XCHG", "XCHG", "MOV ", "MOV ", "MOV ", "MOV ", + "MOV ", "LEA ", "MOV ", "POP ", "NOP ", "XCHA", "XCHA", "XCHA", "XCHA", "XCHA", "XCHA", "XCHA", "CBW ", "CWD ", + "CALL", "WAIT", "PSHF", "POPF", "SAHF", "LAHF", "MOV ", "MOV ", "MOV ", "MOV ", "MOVS", "MOVS", "CMPS", "CMPS", + "TEST", "TEST", "STOS", "STOS", "LODS", "LODS", "SCAS", "SCAS", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", + "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "MOV ", "GRP2", "GRP2", "RETN", "RETN", + "LES ", "LDS ", "MOV ", "MOV ", "ENTR", "LEAV", "RETF", "RETF", "INT3", "INT ", "INTO", "IRET", "GRP2", "GRP2", + "GRP2", "GRP2", "AAM ", "AAD ", "----", "XLAT", "----", "----", "----", "----", "----", "----", "----", "----", + "LPNZ", "LPZ ", "LOOP", "JCXZ", "IN ", "IN ", "OUT ", "OUT ", "CALL", "JMP ", "JMP ", "JMP ", "IN ", "IN ", + "OUT ", "OUT ", "LOCK", "----", "RPNZ", "REP ", "HLT ", "CMC ", "GP3A", "GP3B", "CLC ", "STC ", "CLI ", "STI ", + "CLD ", "STD ", "GPR4", "GRP5" }; int nec_execute(int cycles) { - int done; + int done; - nec_ICount=cycles; -// cpu_type=V30; + nec_ICount = cycles; + // cpu_type=V30; - while(nec_ICount>0) - { + while (nec_ICount > 0) + { #if 0 - uint8_t op = cpu_readmem20((I.sregs[CS]<<4) + I.ip); + uint8_t op = cpu_readmem20((I.sregs[CS]<<4) + I.ip); printf("[%04x:%04xh] %02xh '%s' - I=%d\n", I.sregs[CS], I.ip, op, instructionsName[op], I.IF); #endif - nec_instruction[FETCHOP](); -// nec_ICount++; - } + nec_instruction[FETCHOP](); + // nec_ICount++; + } - done = cycles - nec_ICount; + done = cycles - nec_ICount; - nec_monotonicCycles += done; + nec_monotonicCycles += done; - return done; + return done; } diff --git a/source/nec/nec.h b/source/nec/nec.h index aed4970..1c9e680 100644 --- a/source/nec/nec.h +++ b/source/nec/nec.h @@ -1,37 +1,54 @@ +/* + * NewOswan + * nec.h: + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ #ifndef __NEC_H_ #define __NEC_H_ #include #include "necintrf.h" -typedef enum { ES, CS, SS, DS } SREGS; -typedef enum { AW, CW, DW, BW, SP, BP, IX, IY } WREGS; -typedef enum { AL,AH,CL,CH,DL,DH,BL,BH,SPL,SPH,BPL,BPH,IXL,IXH,IYL,IYH } BREGS; +typedef enum +{ + ES, CS, SS, DS +} SREGS; +typedef enum +{ + AW, CW, DW, BW, SP, BP, IX, IY +} WREGS; +typedef enum +{ + AL, AH, CL, CH, DL, DH, BL, BH, SPL, SPH, BPL, BPH, IXL, IXH, IYL, IYH +} BREGS; #pragma pack(1) typedef union { - /* eight general registers */ - uint16_t w[8]; /* viewed as 16 bits registers */ - uint8_t b[16]; /* or as 8 bit registers */ + /* eight general registers */ + uint16_t w[8]; /* viewed as 16 bits registers */ + uint8_t b[16]; /* or as 8 bit registers */ } necbasicregs; + typedef struct { - necbasicregs regs; - uint16_t sregs[4]; + necbasicregs regs; + uint16_t sregs[4]; - uint16_t ip; + uint16_t ip; - int32_t SignVal; - int32_t AuxVal, OverVal, ZeroVal, CarryVal, ParityVal; /* 0 or non-0 valued flags */ + int32_t SignVal; + int32_t AuxVal, OverVal, ZeroVal, CarryVal, ParityVal; /* 0 or non-0 valued flags */ - uint32_t TF, IF, DF, MF; /* 0 or 1 valued flags */ /* OB[19.07.99] added Mode Flag V30 */ + uint32_t TF, IF, DF, MF; /* 0 or 1 valued flags */ /* OB[19.07.99] added Mode Flag V30 */ - uint32_t int_vector; - uint32_t pending_irq; - uint32_t nmi_state; - uint32_t irq_state; - int (*irq_callback)(int irqline); + uint32_t int_vector; + uint32_t pending_irq; + uint32_t nmi_state; + uint32_t irq_state; + int (*irq_callback)(int irqline); } nec_Regs; #pragma pack() @@ -49,30 +66,28 @@ typedef struct /* parameter x = result, y = source 1, z = source 2 */ -#define SetTF(x) (I.TF = (x)) -#define SetIF(x) (I.IF = (x)) -#define SetDF(x) (I.DF = (x)) -#define SetMD(x) (I.MF = (x)) /* OB [19.07.99] Mode Flag V30 */ +#define SetTF(x) (I.TF = (x)) +#define SetIF(x) (I.IF = (x)) +#define SetDF(x) (I.DF = (x)) +#define SetMD(x) (I.MF = (x)) /* OB [19.07.99] Mode Flag V30 */ -#define SetCFB(x) (I.CarryVal = (x) & 0x100) -#define SetCFW(x) (I.CarryVal = (x) & 0x10000) +#define SetCFB(x) (I.CarryVal = (x) & 0x100) +#define SetCFW(x) (I.CarryVal = (x) & 0x10000) -#define SetAF(x,y,z) (I.AuxVal = ((x) ^ ((y) ^ (z))) & 0x10) +#define SetAF(x, y, z) (I.AuxVal = ((x) ^ ((y) ^ (z))) & 0x10) - - -#define SetSF(x) (I.SignVal = (x)) -#define SetZF(x) (I.ZeroVal = (x)) -#define SetPF(x) (I.ParityVal = (x)) +#define SetSF(x) (I.SignVal = (x)) +#define SetZF(x) (I.ZeroVal = (x)) +#define SetPF(x) (I.ParityVal = (x)) #define SetSZPF_Byte(x) (I.SignVal=I.ZeroVal=I.ParityVal=(int8_t)(x)) #define SetSZPF_Word(x) (I.SignVal=I.ZeroVal=I.ParityVal=(int16_t)(x)) -#define SetOFW_Add(x,y,z) (I.OverVal = ((x) ^ (y)) & ((x) ^ (z)) & 0x8000) -#define SetOFB_Add(x,y,z) (I.OverVal = ((x) ^ (y)) & ((x) ^ (z)) & 0x80) -#define SetOFW_Sub(x,y,z) (I.OverVal = ((z) ^ (y)) & ((z) ^ (x)) & 0x8000) -#define SetOFB_Sub(x,y,z) (I.OverVal = ((z) ^ (y)) & ((z) ^ (x)) & 0x80) +#define SetOFW_Add(x, y, z) (I.OverVal = ((x) ^ (y)) & ((x) ^ (z)) & 0x8000) +#define SetOFB_Add(x, y, z) (I.OverVal = ((x) ^ (y)) & ((x) ^ (z)) & 0x80) +#define SetOFW_Sub(x, y, z) (I.OverVal = ((z) ^ (y)) & ((z) ^ (x)) & 0x8000) +#define SetOFB_Sub(x, y, z) (I.OverVal = ((z) ^ (y)) & ((z) ^ (x)) & 0x80) #define ADDB { uint32_t res=dst+src; SetCFB(res); SetOFB_Add(res,src,dst); SetAF(res,src,dst); SetSZPF_Byte(res); dst=(uint8_t)res; } #define ADDW { uint32_t res=dst+src; SetCFW(res); SetOFW_Add(res,src,dst); SetAF(res,src,dst); SetSZPF_Word(res); dst=(uint16_t)res; } @@ -89,13 +104,13 @@ typedef struct #define XORB dst^=src; I.CarryVal=I.OverVal=I.AuxVal=0; SetSZPF_Byte(dst) #define XORW dst^=src; I.CarryVal=I.OverVal=I.AuxVal=0; SetSZPF_Word(dst) -#define CF (I.CarryVal!=0) -#define SF (I.SignVal<0) -#define ZF (I.ZeroVal==0) -#define PF parity_table[(uint8_t)I.ParityVal] -#define AF (I.AuxVal!=0) -#define OF (I.OverVal!=0) -#define MD (I.MF!=0) +#define CF (I.CarryVal!=0) +#define SF (I.SignVal<0) +#define ZF (I.ZeroVal==0) +#define PF parity_table[(uint8_t)I.ParityVal] +#define AF (I.AuxVal!=0) +#define OF (I.OverVal!=0) +#define MD (I.MF!=0) /************************************************************************/ @@ -103,20 +118,20 @@ typedef struct #define DefaultBase(Seg) ((seg_prefix && (Seg==DS || Seg==SS)) ? prefix_base : I.sregs[Seg] << 4) -#define GetMemB(Seg,Off) (/*nec_ICount-=((Off)&1)?1:0,*/ (uint8_t)cpu_readmem20((DefaultBase(Seg)+(Off)))) -#define GetMemW(Seg,Off) (/*nec_ICount-=((Off)&1)?1:0,*/ (uint16_t) cpu_readmem20((DefaultBase(Seg)+(Off))) + (cpu_readmem20((DefaultBase(Seg)+((Off)+1)))<<8) ) +#define GetMemB(Seg, Off) (/*nec_ICount-=((Off)&1)?1:0,*/ (uint8_t)cpu_readmem20((DefaultBase(Seg)+(Off)))) +#define GetMemW(Seg, Off) (/*nec_ICount-=((Off)&1)?1:0,*/ (uint16_t) cpu_readmem20((DefaultBase(Seg)+(Off))) + (cpu_readmem20((DefaultBase(Seg)+((Off)+1)))<<8) ) -#define PutMemB(Seg,Off,x) { /*nec_ICount-=((Off)&1)?1:0*/; cpu_writemem20((DefaultBase(Seg)+(Off)),(x)); } -#define PutMemW(Seg,Off,x) { /*nec_ICount-=((Off)&1)?1:0*/; PutMemB(Seg,Off,(x)&0xff); PutMemB(Seg,(Off)+1,(uint8_t)((x)>>8)); } +#define PutMemB(Seg, Off, x) { /*nec_ICount-=((Off)&1)?1:0*/; cpu_writemem20((DefaultBase(Seg)+(Off)),(x)); } +#define PutMemW(Seg, Off, x) { /*nec_ICount-=((Off)&1)?1:0*/; PutMemB(Seg,Off,(x)&0xff); PutMemB(Seg,(Off)+1,(uint8_t)((x)>>8)); } /* Todo: Remove these later - plus readword could overflow */ #define ReadByte(ea) (/*nec_ICount-=((ea)&1)?1:0,*/ (uint8_t)cpu_readmem20((ea))) #define ReadWord(ea) (/*nec_ICount-=((ea)&1)?1:0,*/ cpu_readmem20((ea))+(cpu_readmem20(((ea)+1))<<8)) -#define WriteByte(ea,val) { /*nec_ICount-=((ea)&1)?1:0*/; cpu_writemem20((ea),val); } -#define WriteWord(ea,val) { /*nec_ICount-=((ea)&1)?1:0*/; cpu_writemem20((ea),(uint8_t)(val)); cpu_writemem20(((ea)+1),(val)>>8); } +#define WriteByte(ea, val) { /*nec_ICount-=((ea)&1)?1:0*/; cpu_writemem20((ea),val); } +#define WriteWord(ea, val) { /*nec_ICount-=((ea)&1)?1:0*/; cpu_writemem20((ea),(uint8_t)(val)); cpu_writemem20(((ea)+1),(val)>>8); } #define read_port(port) cpu_readport(port) -#define write_port(port,val) cpu_writeport(port,val) +#define write_port(port, val) cpu_writeport(port,val) #define FETCH (cpu_readop_arg((I.sregs[CS]<<4)+I.ip++)) #define FETCHOP (cpu_readop((I.sregs[CS]<<4)+I.ip++)) @@ -145,121 +160,119 @@ typedef struct #define CLKM(v20,v30,v33,v20m,v30m,v33m) { const uint32_t ccount=(v20<<16)|(v30<<8)|v33, mcount=(v20m<<16)|(v30m<<8)|v33m; nec_ICount-=( ModRM >=0xc0 )?((ccount>>cpu_type)&0x7f):((mcount>>cpu_type)&0x7f); } #define CLKR(v20o,v30o,v33o,v20e,v30e,v33e,vall) { const uint32_t ocount=(v20o<<16)|(v30o<<8)|v33o, ecount=(v20e<<16)|(v30e<<8)|v33e; if (ModRM >=0xc0) nec_ICount-=vall; else nec_ICount-=(I.ip&1)?((ocount>>cpu_type)&0x7f):((ecount>>cpu_type)&0x7f); } */ -#define CLKS(v20,v30,v33) { const uint32_t ccount=(v20<<16)|(v30<<8)|v33; nec_ICount-=(ccount>>cpu_type)&0x7f; } +#define CLKS(v20, v30, v33) { const uint32_t ccount=(v20<<16)|(v30<<8)|v33; nec_ICount-=(ccount>>cpu_type)&0x7f; } #define CLK(all) nec_ICount-=all -#define CLKW(v30MZo,v30MZe) { nec_ICount-=(I.ip&1)?v30MZo:v30MZe; } -#define CLKM(v30MZm,v30MZ) { nec_ICount-=( ModRM >=0xc0 )?v30MZ:v30MZm; } -#define CLKR(v30MZo,v30MZe,vall) { if (ModRM >=0xc0) nec_ICount-=vall; else nec_ICount-=(I.ip&1)?v30MZo:v30MZe; } +#define CLKW(v30MZo, v30MZe) { nec_ICount-=(I.ip&1)?v30MZo:v30MZe; } +#define CLKM(v30MZm, v30MZ) { nec_ICount-=( ModRM >=0xc0 )?v30MZ:v30MZm; } +#define CLKR(v30MZo, v30MZe, vall) { if (ModRM >=0xc0) nec_ICount-=vall; else nec_ICount-=(I.ip&1)?v30MZo:v30MZe; } #define CompressFlags() (uint16_t)(CF | (PF << 2) | (AF << 4) | (ZF << 6) \ - | (SF << 7) | (I.TF << 8) | (I.IF << 9) \ - | (I.DF << 10) | (OF << 11)) + | (SF << 7) | (I.TF << 8) | (I.IF << 9) \ + | (I.DF << 10) | (OF << 11)) #define ExpandFlags(f) \ { \ - I.CarryVal = (f) & 1; \ - I.ParityVal = !((f) & 4); \ - I.AuxVal = (f) & 16; \ - I.ZeroVal = !((f) & 64); \ - I.SignVal = (f) & 128 ? -1 : 0; \ - I.TF = ((f) & 256) == 256; \ - I.IF = ((f) & 512) == 512; \ - I.DF = ((f) & 1024) == 1024; \ - I.OverVal = (f) & 2048; \ - I.MF = ((f) & 0x8000) == 0x8000; \ + I.CarryVal = (f) & 1; \ + I.ParityVal = !((f) & 4); \ + I.AuxVal = (f) & 16; \ + I.ZeroVal = !((f) & 64); \ + I.SignVal = (f) & 128 ? -1 : 0; \ + I.TF = ((f) & 256) == 256; \ + I.IF = ((f) & 512) == 512; \ + I.DF = ((f) & 1024) == 1024; \ + I.OverVal = (f) & 2048; \ + I.MF = ((f) & 0x8000) == 0x8000; \ } - -#define IncWordReg(Reg) \ - uint16_t tmp = (uint16_t)I.regs.w[Reg]; \ - uint16_t tmp1 = tmp+1; \ - I.OverVal = (tmp == 0x7fff); \ - SetAF(tmp1,tmp,1); \ - SetSZPF_Word(tmp1); \ - I.regs.w[Reg]=tmp1 +#define IncWordReg(Reg) \ + uint16_t tmp = (uint16_t)I.regs.w[Reg]; \ + uint16_t tmp1 = tmp+1; \ + I.OverVal = (tmp == 0x7fff); \ + SetAF(tmp1,tmp,1); \ + SetSZPF_Word(tmp1); \ + I.regs.w[Reg]=tmp1 +#define DecWordReg(Reg) \ + uint16_t tmp = (uint16_t)I.regs.w[Reg]; \ + uint16_t tmp1 = tmp-1; \ + I.OverVal = (tmp == 0x8000); \ + SetAF(tmp1,tmp,1); \ + SetSZPF_Word(tmp1); \ + I.regs.w[Reg]=tmp1 -#define DecWordReg(Reg) \ - uint16_t tmp = (uint16_t)I.regs.w[Reg]; \ - uint16_t tmp1 = tmp-1; \ - I.OverVal = (tmp == 0x8000); \ - SetAF(tmp1,tmp,1); \ - SetSZPF_Word(tmp1); \ - I.regs.w[Reg]=tmp1 - -#define JMP(flag) \ - int tmp = (int)((int8_t)FETCH); \ - if (flag) \ - { \ - I.ip = (uint16_t)(I.ip+tmp); \ - nec_ICount-=3; \ - return; \ - } - -#define ADJ4(param1,param2) \ - if (AF || ((I.regs.b[AL] & 0xf) > 9)) \ - { \ - int tmp; \ - I.regs.b[AL] = tmp = I.regs.b[AL] + param1; \ - I.AuxVal = 1; \ - } \ - if (CF || (I.regs.b[AL] > 0x9f)) \ - { \ - I.regs.b[AL] += param2; \ - I.CarryVal = 1; \ - } \ - SetSZPF_Byte(I.regs.b[AL]) - -#define ADJB(param1,param2) \ - if (AF || ((I.regs.b[AL] & 0xf) > 9)) \ - { \ - I.regs.b[AL] += param1; \ - I.regs.b[AH] += param2; \ - I.AuxVal = 1; \ - I.CarryVal = 1; \ - } \ - else \ - { \ - I.AuxVal = 0; \ - I.CarryVal = 0; \ - } \ - I.regs.b[AL] &= 0x0F - -#define BITOP_BYTE \ - ModRM = FETCH; \ - if (ModRM >= 0xc0) { \ - tmp=I.regs.b[Mod_RM.RM.b[ModRM]]; \ - } \ - else { \ - (*GetEA[ModRM])(); \ - tmp=ReadByte(EA); \ +#define JMP(flag) \ + int tmp = (int)((int8_t)FETCH); \ + if (flag) \ + { \ + I.ip = (uint16_t)(I.ip+tmp); \ + nec_ICount-=3; \ + return; \ } -#define BITOP_WORD \ - ModRM = FETCH; \ - if (ModRM >= 0xc0) { \ - tmp=I.regs.w[Mod_RM.RM.w[ModRM]]; \ - } \ - else { \ - (*GetEA[ModRM])(); \ - tmp=ReadWord(EA); \ +#define ADJ4(param1, param2) \ + if (AF || ((I.regs.b[AL] & 0xf) > 9)) \ + { \ + int tmp; \ + I.regs.b[AL] = tmp = I.regs.b[AL] + param1; \ + I.AuxVal = 1; \ + } \ + if (CF || (I.regs.b[AL] > 0x9f)) \ + { \ + I.regs.b[AL] += param2; \ + I.CarryVal = 1; \ + } \ + SetSZPF_Byte(I.regs.b[AL]) + +#define ADJB(param1, param2) \ + if (AF || ((I.regs.b[AL] & 0xf) > 9)) \ + { \ + I.regs.b[AL] += param1; \ + I.regs.b[AH] += param2; \ + I.AuxVal = 1; \ + I.CarryVal = 1; \ + } \ + else \ + { \ + I.AuxVal = 0; \ + I.CarryVal = 0; \ + } \ + I.regs.b[AL] &= 0x0F + +#define BITOP_BYTE \ + ModRM = FETCH; \ + if (ModRM >= 0xc0) { \ + tmp=I.regs.b[Mod_RM.RM.b[ModRM]]; \ + } \ + else { \ + (*GetEA[ModRM])(); \ + tmp=ReadByte(EA); \ } -#define BIT_NOT \ - if (tmp & (1<= 0xc0) { \ + tmp=I.regs.w[Mod_RM.RM.w[ModRM]]; \ + } \ + else { \ + (*GetEA[ModRM])(); \ + tmp=ReadWord(EA); \ + } -#define XchgAWReg(Reg) \ - uint16_t tmp; \ - tmp = I.regs.w[Reg]; \ - I.regs.w[Reg] = I.regs.w[AW]; \ - I.regs.w[AW] = tmp +#define BIT_NOT \ + if (tmp & (1<>= 1 #define RORC_WORD dst = (CF<<16)+dst; I.CarryVal = dst & 0x01; dst >>= 1 -#define SHL_BYTE(c) dst <<= c; SetCFB(dst); SetSZPF_Byte(dst); PutbackRMByte(ModRM,(uint8_t)dst) -#define SHL_WORD(c) dst <<= c; SetCFW(dst); SetSZPF_Word(dst); PutbackRMWord(ModRM,(uint16_t)dst) +#define SHL_BYTE(c) dst <<= c; SetCFB(dst); SetSZPF_Byte(dst); PutbackRMByte(ModRM,(uint8_t)dst) +#define SHL_WORD(c) dst <<= c; SetCFW(dst); SetSZPF_Word(dst); PutbackRMWord(ModRM,(uint16_t)dst) #define SHR_BYTE(c) dst >>= c-1; I.CarryVal = dst & 0x1; dst >>= 1; SetSZPF_Byte(dst); PutbackRMByte(ModRM,(uint8_t)dst) #define SHR_WORD(c) dst >>= c-1; I.CarryVal = dst & 0x1; dst >>= 1; SetSZPF_Word(dst); PutbackRMWord(ModRM,(uint16_t)dst) -#define SHRA_BYTE(c) dst = ((int8_t)dst) >> (c-1); I.CarryVal = dst & 0x1; dst = ((int8_t)((uint8_t)dst)) >> 1; SetSZPF_Byte(dst); PutbackRMByte(ModRM,(uint8_t)dst) -#define SHRA_WORD(c) dst = ((int16_t)dst) >> (c-1); I.CarryVal = dst & 0x1; dst = ((int16_t)((uint16_t)dst)) >> 1; SetSZPF_Word(dst); PutbackRMWord(ModRM,(uint16_t)dst) +#define SHRA_BYTE(c) dst = ((int8_t)dst) >> (c-1); I.CarryVal = dst & 0x1; dst = ((int8_t)((uint8_t)dst)) >> 1; SetSZPF_Byte(dst); PutbackRMByte(ModRM,(uint8_t)dst) +#define SHRA_WORD(c) dst = ((int16_t)dst) >> (c-1); I.CarryVal = dst & 0x1; dst = ((int16_t)((uint16_t)dst)) >> 1; SetSZPF_Word(dst); PutbackRMWord(ModRM,(uint16_t)dst) -#define DIVUB \ - uresult = I.regs.w[AW]; \ - uresult2 = uresult % tmp; \ - if ((uresult /= tmp) > 0xff) { \ - nec_interrupt(0,0); break; \ - } else { \ - I.regs.b[AL] = uresult; \ - I.regs.b[AH] = uresult2; \ - } +#define DIVUB \ + uresult = I.regs.w[AW]; \ + uresult2 = uresult % tmp; \ + if ((uresult /= tmp) > 0xff) { \ + nec_interrupt(0,0); break; \ + } else { \ + I.regs.b[AL] = uresult; \ + I.regs.b[AH] = uresult2; \ + } -#define DIVB \ - result = (int16_t)I.regs.w[AW]; \ - result2 = result % (int16_t)((int8_t)tmp); \ - if ((result /= (int16_t)((int8_t)tmp)) > 0xff) { \ - nec_interrupt(0,0); break; \ - } else { \ - I.regs.b[AL] = result; \ - I.regs.b[AH] = result2; \ - } +#define DIVB \ + result = (int16_t)I.regs.w[AW]; \ + result2 = result % (int16_t)((int8_t)tmp); \ + if ((result /= (int16_t)((int8_t)tmp)) > 0xff) { \ + nec_interrupt(0,0); break; \ + } else { \ + I.regs.b[AL] = result; \ + I.regs.b[AH] = result2; \ + } -#define DIVUW \ - uresult = (((uint32_t)I.regs.w[DW]) << 16) | I.regs.w[AW];\ - uresult2 = uresult % tmp; \ - if ((uresult /= tmp) > 0xffff) { \ - nec_interrupt(0,0); break; \ - } else { \ - I.regs.w[AW]=uresult; \ - I.regs.w[DW]=uresult2; \ - } +#define DIVUW \ + uresult = (((uint32_t)I.regs.w[DW]) << 16) | I.regs.w[AW];\ + uresult2 = uresult % tmp; \ + if ((uresult /= tmp) > 0xffff) { \ + nec_interrupt(0,0); break; \ + } else { \ + I.regs.w[AW]=uresult; \ + I.regs.w[DW]=uresult2; \ + } -#define DIVW \ - result = ((uint32_t)I.regs.w[DW] << 16) + I.regs.w[AW]; \ - result2 = result % (int32_t)((int16_t)tmp); \ - if ((result /= (int32_t)((int16_t)tmp)) > 0xffff) { \ - nec_interrupt(0,0); break; \ - } else { \ - I.regs.w[AW]=result; \ - I.regs.w[DW]=result2; \ - } +#define DIVW \ + result = ((uint32_t)I.regs.w[DW] << 16) + I.regs.w[AW]; \ + result2 = result % (int32_t)((int16_t)tmp); \ + if ((result /= (int32_t)((int16_t)tmp)) > 0xffff) { \ + nec_interrupt(0,0); break; \ + } else { \ + I.regs.w[AW]=result; \ + I.regs.w[DW]=result2; \ + } -#define ADD4S { \ - int i,v1,v2,result; \ - int count = (I.regs.b[CL]+1)/2; \ - uint16_t di = I.regs.w[IY]; \ - uint16_t si = I.regs.w[IX]; \ - I.ZeroVal = I.CarryVal = 0; \ - for (i=0;i>4)*10 + (tmp&0xf); \ - v2 = (tmp2>>4)*10 + (tmp2&0xf); \ - result = v1+v2+I.CarryVal; \ - I.CarryVal = result > 99 ? 1 : 0; \ - result = result % 100; \ - v1 = ((result/10)<<4) | (result % 10); \ - PutMemB(ES, di,v1); \ - if (v1) I.ZeroVal = 1; \ - si++; \ - di++; \ - } \ +#define ADD4S { \ + int i,v1,v2,result; \ + int count = (I.regs.b[CL]+1)/2; \ + uint16_t di = I.regs.w[IY]; \ + uint16_t si = I.regs.w[IX]; \ + I.ZeroVal = I.CarryVal = 0; \ + for (i=0;i>4)*10 + (tmp&0xf); \ + v2 = (tmp2>>4)*10 + (tmp2&0xf); \ + result = v1+v2+I.CarryVal; \ + I.CarryVal = result > 99 ? 1 : 0; \ + result = result % 100; \ + v1 = ((result/10)<<4) | (result % 10); \ + PutMemB(ES, di,v1); \ + if (v1) I.ZeroVal = 1; \ + si++; \ + di++; \ + } \ } -#define SUB4S { \ - int count = (I.regs.b[CL]+1)/2; \ - int i,v1,v2,result; \ - uint16_t di = I.regs.w[IY]; \ - uint16_t si = I.regs.w[IX]; \ - I.ZeroVal = I.CarryVal = 0; \ - for (i=0;i>4)*10 + (tmp&0xf); \ - v2 = (tmp2>>4)*10 + (tmp2&0xf); \ - if (v1 < (v2+I.CarryVal)) { \ - v1+=100; \ - result = v1-(v2+I.CarryVal); \ - I.CarryVal = 1; \ - } else { \ - result = v1-(v2+I.CarryVal); \ - I.CarryVal = 0; \ - } \ - v1 = ((result/10)<<4) | (result % 10); \ - PutMemB(ES, di,v1); \ - if (v1) I.ZeroVal = 1; \ - si++; \ - di++; \ - } \ +#define SUB4S { \ + int count = (I.regs.b[CL]+1)/2; \ + int i,v1,v2,result; \ + uint16_t di = I.regs.w[IY]; \ + uint16_t si = I.regs.w[IX]; \ + I.ZeroVal = I.CarryVal = 0; \ + for (i=0;i>4)*10 + (tmp&0xf); \ + v2 = (tmp2>>4)*10 + (tmp2&0xf); \ + if (v1 < (v2+I.CarryVal)) { \ + v1+=100; \ + result = v1-(v2+I.CarryVal); \ + I.CarryVal = 1; \ + } else { \ + result = v1-(v2+I.CarryVal); \ + I.CarryVal = 0; \ + } \ + v1 = ((result/10)<<4) | (result % 10); \ + PutMemB(ES, di,v1); \ + if (v1) I.ZeroVal = 1; \ + si++; \ + di++; \ + } \ } -#define CMP4S { \ - int count = (I.regs.b[CL]+1)/2; \ - int i,v1,v2,result; \ - uint16_t di = I.regs.w[IY]; \ - uint16_t si = I.regs.w[IX]; \ - I.ZeroVal = I.CarryVal = 0; \ - for (i=0;i>4)*10 + (tmp&0xf); \ - v2 = (tmp2>>4)*10 + (tmp2&0xf); \ - if (v1 < (v2+I.CarryVal)) { \ - v1+=100; \ - result = v1-(v2+I.CarryVal); \ - I.CarryVal = 1; \ - } else { \ - result = v1-(v2+I.CarryVal); \ - I.CarryVal = 0; \ - } \ - v1 = ((result/10)<<4) | (result % 10); \ - if (v1) I.ZeroVal = 1; \ - si++; \ - di++; \ - } \ +#define CMP4S { \ + int count = (I.regs.b[CL]+1)/2; \ + int i,v1,v2,result; \ + uint16_t di = I.regs.w[IY]; \ + uint16_t si = I.regs.w[IX]; \ + I.ZeroVal = I.CarryVal = 0; \ + for (i=0;i>4)*10 + (tmp&0xf); \ + v2 = (tmp2>>4)*10 + (tmp2&0xf); \ + if (v1 < (v2+I.CarryVal)) { \ + v1+=100; \ + result = v1-(v2+I.CarryVal); \ + I.CarryVal = 1; \ + } else { \ + result = v1-(v2+I.CarryVal); \ + I.CarryVal = 0; \ + } \ + v1 = ((result/10)<<4) | (result % 10); \ + if (v1) I.ZeroVal = 1; \ + si++; \ + di++; \ + } \ } #endif /* __NEC_H_ */ \ No newline at end of file diff --git a/source/nec/necea.h b/source/nec/necea.h index 0e380f2..80b7e0d 100644 --- a/source/nec/necea.h +++ b/source/nec/necea.h @@ -1,3 +1,10 @@ +/* + * NewOswan + * necea.h: + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ static uint32_t EA; static uint16_t EO; @@ -5,194 +12,206 @@ static uint16_t E16; static unsigned EA_000(void) { - EO=I.regs.w[BW]+I.regs.w[IX]; - EA=DefaultBase(DS)+EO; - return EA; + EO = I.regs.w[BW] + I.regs.w[IX]; + EA = DefaultBase(DS) + EO; + return EA; } + static unsigned EA_001(void) { - EO=I.regs.w[BW]+I.regs.w[IY]; - EA=DefaultBase(DS)+EO; - return EA; + EO = I.regs.w[BW] + I.regs.w[IY]; + EA = DefaultBase(DS) + EO; + return EA; } + static unsigned EA_002(void) { - EO=I.regs.w[BP]+I.regs.w[IX]; - EA=DefaultBase(SS)+EO; - return EA; + EO = I.regs.w[BP] + I.regs.w[IX]; + EA = DefaultBase(SS) + EO; + return EA; } + static unsigned EA_003(void) { - EO=I.regs.w[BP]+I.regs.w[IY]; - EA=DefaultBase(SS)+EO; - return EA; + EO = I.regs.w[BP] + I.regs.w[IY]; + EA = DefaultBase(SS) + EO; + return EA; } + static unsigned EA_004(void) { - EO=I.regs.w[IX]; - EA=DefaultBase(DS)+EO; - return EA; + EO = I.regs.w[IX]; + EA = DefaultBase(DS) + EO; + return EA; } + static unsigned EA_005(void) { - EO=I.regs.w[IY]; - EA=DefaultBase(DS)+EO; - return EA; + EO = I.regs.w[IY]; + EA = DefaultBase(DS) + EO; + return EA; } + static unsigned EA_006(void) { - EO=FETCH; - EO+=FETCH<<8; - EA=DefaultBase(DS)+EO; - return EA; + EO = FETCH; + EO += FETCH << 8; + EA = DefaultBase(DS) + EO; + return EA; } + static unsigned EA_007(void) { - EO=I.regs.w[BW]; - EA=DefaultBase(DS)+EO; - return EA; + EO = I.regs.w[BW]; + EA = DefaultBase(DS) + EO; + return EA; } static unsigned EA_100(void) { - EO=(I.regs.w[BW]+I.regs.w[IX]+(int8_t)FETCH); - EA=DefaultBase(DS)+EO; - return EA; + EO = (I.regs.w[BW] + I.regs.w[IX] + (int8_t)FETCH); + EA = DefaultBase(DS) + EO; + return EA; } + static unsigned EA_101(void) { - EO=(I.regs.w[BW]+I.regs.w[IY]+(int8_t)FETCH); - EA=DefaultBase(DS)+EO; - return EA; + EO = (I.regs.w[BW] + I.regs.w[IY] + (int8_t)FETCH); + EA = DefaultBase(DS) + EO; + return EA; } + static unsigned EA_102(void) { - EO=(I.regs.w[BP]+I.regs.w[IX]+(int8_t)FETCH); - EA=DefaultBase(SS)+EO; - return EA; + EO = (I.regs.w[BP] + I.regs.w[IX] + (int8_t)FETCH); + EA = DefaultBase(SS) + EO; + return EA; } + static unsigned EA_103(void) { - EO=(I.regs.w[BP]+I.regs.w[IY]+(int8_t)FETCH); - EA=DefaultBase(SS)+EO; - return EA; + EO = (I.regs.w[BP] + I.regs.w[IY] + (int8_t)FETCH); + EA = DefaultBase(SS) + EO; + return EA; } + static unsigned EA_104(void) { - EO=(I.regs.w[IX]+(int8_t)FETCH); - EA=DefaultBase(DS)+EO; - return EA; + EO = (I.regs.w[IX] + (int8_t)FETCH); + EA = DefaultBase(DS) + EO; + return EA; } + static unsigned EA_105(void) { - EO=(I.regs.w[IY]+(int8_t)FETCH); - EA=DefaultBase(DS)+EO; - return EA; + EO = (I.regs.w[IY] + (int8_t)FETCH); + EA = DefaultBase(DS) + EO; + return EA; } + static unsigned EA_106(void) { - EO=(I.regs.w[BP]+(int8_t)FETCH); - EA=DefaultBase(SS)+EO; - return EA; + EO = (I.regs.w[BP] + (int8_t)FETCH); + EA = DefaultBase(SS) + EO; + return EA; } + static unsigned EA_107(void) { - EO=(I.regs.w[BW]+(int8_t)FETCH); - EA=DefaultBase(DS)+EO; - return EA; + EO = (I.regs.w[BW] + (int8_t)FETCH); + EA = DefaultBase(DS) + EO; + return EA; } static unsigned EA_200(void) { - E16=FETCH; - E16+=FETCH<<8; - EO=I.regs.w[BW]+I.regs.w[IX]+(int16_t)E16; - EA=DefaultBase(DS)+EO; - return EA; + E16 = FETCH; + E16 += FETCH << 8; + EO = I.regs.w[BW] + I.regs.w[IX] + (int16_t)E16; + EA = DefaultBase(DS) + EO; + return EA; } + static unsigned EA_201(void) { - E16=FETCH; - E16+=FETCH<<8; - EO=I.regs.w[BW]+I.regs.w[IY]+(int16_t)E16; - EA=DefaultBase(DS)+EO; - return EA; + E16 = FETCH; + E16 += FETCH << 8; + EO = I.regs.w[BW] + I.regs.w[IY] + (int16_t)E16; + EA = DefaultBase(DS) + EO; + return EA; } + static unsigned EA_202(void) { - E16=FETCH; - E16+=FETCH<<8; - EO=I.regs.w[BP]+I.regs.w[IX]+(int16_t)E16; - EA=DefaultBase(SS)+EO; - return EA; + E16 = FETCH; + E16 += FETCH << 8; + EO = I.regs.w[BP] + I.regs.w[IX] + (int16_t)E16; + EA = DefaultBase(SS) + EO; + return EA; } + static unsigned EA_203(void) { - E16=FETCH; - E16+=FETCH<<8; - EO=I.regs.w[BP]+I.regs.w[IY]+(int16_t)E16; - EA=DefaultBase(SS)+EO; - return EA; + E16 = FETCH; + E16 += FETCH << 8; + EO = I.regs.w[BP] + I.regs.w[IY] + (int16_t)E16; + EA = DefaultBase(SS) + EO; + return EA; } + static unsigned EA_204(void) { - E16=FETCH; - E16+=FETCH<<8; - EO=I.regs.w[IX]+(int16_t)E16; - EA=DefaultBase(DS)+EO; - return EA; + E16 = FETCH; + E16 += FETCH << 8; + EO = I.regs.w[IX] + (int16_t)E16; + EA = DefaultBase(DS) + EO; + return EA; } + static unsigned EA_205(void) { - E16=FETCH; - E16+=FETCH<<8; - EO=I.regs.w[IY]+(int16_t)E16; - EA=DefaultBase(DS)+EO; - return EA; + E16 = FETCH; + E16 += FETCH << 8; + EO = I.regs.w[IY] + (int16_t)E16; + EA = DefaultBase(DS) + EO; + return EA; } + static unsigned EA_206(void) { - E16=FETCH; - E16+=FETCH<<8; - EO=I.regs.w[BP]+(int16_t)E16; - EA=DefaultBase(SS)+EO; - return EA; + E16 = FETCH; + E16 += FETCH << 8; + EO = I.regs.w[BP] + (int16_t)E16; + EA = DefaultBase(SS) + EO; + return EA; } + static unsigned EA_207(void) { - E16=FETCH; - E16+=FETCH<<8; - EO=I.regs.w[BW]+(int16_t)E16; - EA=DefaultBase(DS)+EO; - return EA; + E16 = FETCH; + E16 += FETCH << 8; + EO = I.regs.w[BW] + (int16_t)E16; + EA = DefaultBase(DS) + EO; + return EA; } -static unsigned (*GetEA[192])(void)= -{ - EA_000, EA_001, EA_002, EA_003, EA_004, EA_005, EA_006, EA_007, - EA_000, EA_001, EA_002, EA_003, EA_004, EA_005, EA_006, EA_007, - EA_000, EA_001, EA_002, EA_003, EA_004, EA_005, EA_006, EA_007, - EA_000, EA_001, EA_002, EA_003, EA_004, EA_005, EA_006, EA_007, - EA_000, EA_001, EA_002, EA_003, EA_004, EA_005, EA_006, EA_007, - EA_000, EA_001, EA_002, EA_003, EA_004, EA_005, EA_006, EA_007, - EA_000, EA_001, EA_002, EA_003, EA_004, EA_005, EA_006, EA_007, - EA_000, EA_001, EA_002, EA_003, EA_004, EA_005, EA_006, EA_007, +static unsigned (*GetEA[192])(void) = + { + EA_000, EA_001, EA_002, EA_003, EA_004, EA_005, EA_006, EA_007, EA_000, EA_001, EA_002, EA_003, EA_004, EA_005, + EA_006, EA_007, EA_000, EA_001, EA_002, EA_003, EA_004, EA_005, EA_006, EA_007, EA_000, EA_001, EA_002, EA_003, + EA_004, EA_005, EA_006, EA_007, EA_000, EA_001, EA_002, EA_003, EA_004, EA_005, EA_006, EA_007, EA_000, EA_001, + EA_002, EA_003, EA_004, EA_005, EA_006, EA_007, EA_000, EA_001, EA_002, EA_003, EA_004, EA_005, EA_006, EA_007, + EA_000, EA_001, EA_002, EA_003, EA_004, EA_005, EA_006, EA_007, - EA_100, EA_101, EA_102, EA_103, EA_104, EA_105, EA_106, EA_107, - EA_100, EA_101, EA_102, EA_103, EA_104, EA_105, EA_106, EA_107, - EA_100, EA_101, EA_102, EA_103, EA_104, EA_105, EA_106, EA_107, - EA_100, EA_101, EA_102, EA_103, EA_104, EA_105, EA_106, EA_107, - EA_100, EA_101, EA_102, EA_103, EA_104, EA_105, EA_106, EA_107, - EA_100, EA_101, EA_102, EA_103, EA_104, EA_105, EA_106, EA_107, - EA_100, EA_101, EA_102, EA_103, EA_104, EA_105, EA_106, EA_107, - EA_100, EA_101, EA_102, EA_103, EA_104, EA_105, EA_106, EA_107, + EA_100, EA_101, EA_102, EA_103, EA_104, EA_105, EA_106, EA_107, EA_100, EA_101, EA_102, EA_103, EA_104, EA_105, + EA_106, EA_107, EA_100, EA_101, EA_102, EA_103, EA_104, EA_105, EA_106, EA_107, EA_100, EA_101, EA_102, EA_103, + EA_104, EA_105, EA_106, EA_107, EA_100, EA_101, EA_102, EA_103, EA_104, EA_105, EA_106, EA_107, EA_100, EA_101, + EA_102, EA_103, EA_104, EA_105, EA_106, EA_107, EA_100, EA_101, EA_102, EA_103, EA_104, EA_105, EA_106, EA_107, + EA_100, EA_101, EA_102, EA_103, EA_104, EA_105, EA_106, EA_107, - EA_200, EA_201, EA_202, EA_203, EA_204, EA_205, EA_206, EA_207, - EA_200, EA_201, EA_202, EA_203, EA_204, EA_205, EA_206, EA_207, - EA_200, EA_201, EA_202, EA_203, EA_204, EA_205, EA_206, EA_207, - EA_200, EA_201, EA_202, EA_203, EA_204, EA_205, EA_206, EA_207, - EA_200, EA_201, EA_202, EA_203, EA_204, EA_205, EA_206, EA_207, - EA_200, EA_201, EA_202, EA_203, EA_204, EA_205, EA_206, EA_207, - EA_200, EA_201, EA_202, EA_203, EA_204, EA_205, EA_206, EA_207, - EA_200, EA_201, EA_202, EA_203, EA_204, EA_205, EA_206, EA_207 -}; + EA_200, EA_201, EA_202, EA_203, EA_204, EA_205, EA_206, EA_207, EA_200, EA_201, EA_202, EA_203, EA_204, EA_205, + EA_206, EA_207, EA_200, EA_201, EA_202, EA_203, EA_204, EA_205, EA_206, EA_207, EA_200, EA_201, EA_202, EA_203, + EA_204, EA_205, EA_206, EA_207, EA_200, EA_201, EA_202, EA_203, EA_204, EA_205, EA_206, EA_207, EA_200, EA_201, + EA_202, EA_203, EA_204, EA_205, EA_206, EA_207, EA_200, EA_201, EA_202, EA_203, EA_204, EA_205, EA_206, EA_207, + EA_200, EA_201, EA_202, EA_203, EA_204, EA_205, EA_206, EA_207 + }; diff --git a/source/nec/necinstr.h b/source/nec/necinstr.h index 5bf6205..b008d71 100644 --- a/source/nec/necinstr.h +++ b/source/nec/necinstr.h @@ -1,3 +1,11 @@ +/* + * NewOswan + * necinstr.h: + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ + static void i_add_br8(void); static void i_add_wr16(void); static void i_add_r8b(void); @@ -247,261 +255,261 @@ static void i_ffpre(void); static void i_wait(void); void (*nec_instruction[256])(void) = -{ - i_add_br8, /* 0x00 */ - i_add_wr16, /* 0x01 */ - i_add_r8b, /* 0x02 */ - i_add_r16w, /* 0x03 */ - i_add_ald8, /* 0x04 */ - i_add_axd16, /* 0x05 */ - i_push_es, /* 0x06 */ - i_pop_es, /* 0x07 */ - i_or_br8, /* 0x08 */ - i_or_wr16, /* 0x09 */ - i_or_r8b, /* 0x0a */ - i_or_r16w, /* 0x0b */ - i_or_ald8, /* 0x0c */ - i_or_axd16, /* 0x0d */ - i_push_cs, /* 0x0e */ - i_pre_nec /* 0x0f */, - i_adc_br8, /* 0x10 */ - i_adc_wr16, /* 0x11 */ - i_adc_r8b, /* 0x12 */ - i_adc_r16w, /* 0x13 */ - i_adc_ald8, /* 0x14 */ - i_adc_axd16, /* 0x15 */ - i_push_ss, /* 0x16 */ - i_pop_ss, /* 0x17 */ - i_sbb_br8, /* 0x18 */ - i_sbb_wr16, /* 0x19 */ - i_sbb_r8b, /* 0x1a */ - i_sbb_r16w, /* 0x1b */ - i_sbb_ald8, /* 0x1c */ - i_sbb_axd16, /* 0x1d */ - i_push_ds, /* 0x1e */ - i_pop_ds, /* 0x1f */ - i_and_br8, /* 0x20 */ - i_and_wr16, /* 0x21 */ - i_and_r8b, /* 0x22 */ - i_and_r16w, /* 0x23 */ - i_and_ald8, /* 0x24 */ - i_and_axd16, /* 0x25 */ - i_es, /* 0x26 */ - i_daa, /* 0x27 */ - i_sub_br8, /* 0x28 */ - i_sub_wr16, /* 0x29 */ - i_sub_r8b, /* 0x2a */ - i_sub_r16w, /* 0x2b */ - i_sub_ald8, /* 0x2c */ - i_sub_axd16, /* 0x2d */ - i_cs, /* 0x2e */ - i_das, /* 0x2f */ - i_xor_br8, /* 0x30 */ - i_xor_wr16, /* 0x31 */ - i_xor_r8b, /* 0x32 */ - i_xor_r16w, /* 0x33 */ - i_xor_ald8, /* 0x34 */ - i_xor_axd16, /* 0x35 */ - i_ss, /* 0x36 */ - i_aaa, /* 0x37 */ - i_cmp_br8, /* 0x38 */ - i_cmp_wr16, /* 0x39 */ - i_cmp_r8b, /* 0x3a */ - i_cmp_r16w, /* 0x3b */ - i_cmp_ald8, /* 0x3c */ - i_cmp_axd16, /* 0x3d */ - i_ds, /* 0x3e */ - i_aas, /* 0x3f */ - i_inc_ax, /* 0x40 */ - i_inc_cx, /* 0x41 */ - i_inc_dx, /* 0x42 */ - i_inc_bx, /* 0x43 */ - i_inc_sp, /* 0x44 */ - i_inc_bp, /* 0x45 */ - i_inc_si, /* 0x46 */ - i_inc_di, /* 0x47 */ - i_dec_ax, /* 0x48 */ - i_dec_cx, /* 0x49 */ - i_dec_dx, /* 0x4a */ - i_dec_bx, /* 0x4b */ - i_dec_sp, /* 0x4c */ - i_dec_bp, /* 0x4d */ - i_dec_si, /* 0x4e */ - i_dec_di, /* 0x4f */ - i_push_ax, /* 0x50 */ - i_push_cx, /* 0x51 */ - i_push_dx, /* 0x52 */ - i_push_bx, /* 0x53 */ - i_push_sp, /* 0x54 */ - i_push_bp, /* 0x55 */ - i_push_si, /* 0x56 */ - i_push_di, /* 0x57 */ - i_pop_ax, /* 0x58 */ - i_pop_cx, /* 0x59 */ - i_pop_dx, /* 0x5a */ - i_pop_bx, /* 0x5b */ - i_pop_sp, /* 0x5c */ - i_pop_bp, /* 0x5d */ - i_pop_si, /* 0x5e */ - i_pop_di, /* 0x5f */ - i_pusha, /* 0x60 */ - i_popa, /* 0x61 */ - i_chkind, /* 0x62 */ - i_invalid, /* 0x63 */ - i_repnc, /* 0x64 */ - i_repc, /* 0x65 */ - i_invalid, /* 0x66 */ - i_invalid, /* 0x67 */ - i_push_d16, /* 0x68 */ - i_imul_d16, /* 0x69 */ - i_push_d8, /* 0x6a */ - i_imul_d8, /* 0x6b */ - i_insb, /* 0x6c */ - i_insw, /* 0x6d */ - i_outsb, /* 0x6e */ - i_outsw, /* 0x6f */ - i_jo, /* 0x70 */ - i_jno, /* 0x71 */ - i_jc, /* 0x72 */ - i_jnc, /* 0x73 */ - i_jz, /* 0x74 */ - i_jnz, /* 0x75 */ - i_jce, /* 0x76 */ - i_jnce, /* 0x77 */ - i_js, /* 0x78 */ - i_jns, /* 0x79 */ - i_jp, /* 0x7a */ - i_jnp, /* 0x7b */ - i_jl, /* 0x7c */ - i_jnl, /* 0x7d */ - i_jle, /* 0x7e */ - i_jnle, /* 0x7f */ - i_80pre, /* 0x80 */ - i_81pre, /* 0x81 */ - i_82pre, /* 0x82 */ - i_83pre, /* 0x83 */ - i_test_br8, /* 0x84 */ - i_test_wr16, /* 0x85 */ - i_xchg_br8, /* 0x86 */ - i_xchg_wr16, /* 0x87 */ - i_mov_br8, /* 0x88 */ - i_mov_wr16, /* 0x89 */ - i_mov_r8b, /* 0x8a */ - i_mov_r16w, /* 0x8b */ - i_mov_wsreg, /* 0x8c */ - i_lea, /* 0x8d */ - i_mov_sregw, /* 0x8e */ - i_popw, /* 0x8f */ - i_nop, /* 0x90 */ - i_xchg_axcx, /* 0x91 */ - i_xchg_axdx, /* 0x92 */ - i_xchg_axbx, /* 0x93 */ - i_xchg_axsp, /* 0x94 */ - i_xchg_axbp, /* 0x95 */ - i_xchg_axsi, /* 0x97 */ - i_xchg_axdi, /* 0x97 */ - i_cbw, /* 0x98 */ - i_cwd, /* 0x99 */ - i_call_far, /* 0x9a */ - i_wait, /* 0x9b */ - i_pushf, /* 0x9c */ - i_popf, /* 0x9d */ - i_sahf, /* 0x9e */ - i_lahf, /* 0x9f */ - i_mov_aldisp, /* 0xa0 */ - i_mov_axdisp, /* 0xa1 */ - i_mov_dispal, /* 0xa2 */ - i_mov_dispax, /* 0xa3 */ - i_movsb, /* 0xa4 */ - i_movsw, /* 0xa5 */ - i_cmpsb, /* 0xa6 */ - i_cmpsw, /* 0xa7 */ - i_test_ald8, /* 0xa8 */ - i_test_axd16, /* 0xa9 */ - i_stosb, /* 0xaa */ - i_stosw, /* 0xab */ - i_lodsb, /* 0xac */ - i_lodsw, /* 0xad */ - i_scasb, /* 0xae */ - i_scasw, /* 0xaf */ - i_mov_ald8, /* 0xb0 */ - i_mov_cld8, /* 0xb1 */ - i_mov_dld8, /* 0xb2 */ - i_mov_bld8, /* 0xb3 */ - i_mov_ahd8, /* 0xb4 */ - i_mov_chd8, /* 0xb5 */ - i_mov_dhd8, /* 0xb6 */ - i_mov_bhd8, /* 0xb7 */ - i_mov_axd16, /* 0xb8 */ - i_mov_cxd16, /* 0xb9 */ - i_mov_dxd16, /* 0xba */ - i_mov_bxd16, /* 0xbb */ - i_mov_spd16, /* 0xbc */ - i_mov_bpd16, /* 0xbd */ - i_mov_sid16, /* 0xbe */ - i_mov_did16, /* 0xbf */ - i_rotshft_bd8, /* 0xc0 */ - i_rotshft_wd8, /* 0xc1 */ - i_ret_d16, /* 0xc2 */ - i_ret, /* 0xc3 */ - i_les_dw, /* 0xc4 */ - i_lds_dw, /* 0xc5 */ - i_mov_bd8, /* 0xc6 */ - i_mov_wd16, /* 0xc7 */ - i_enter, /* 0xc8 */ - i_leave, /* 0xc9 */ - i_retf_d16, /* 0xca */ - i_retf, /* 0xcb */ - i_int3, /* 0xcc */ - i_int, /* 0xcd */ - i_into, /* 0xce */ - i_iret, /* 0xcf */ - i_rotshft_b, /* 0xd0 */ - i_rotshft_w, /* 0xd1 */ - i_rotshft_bcl, /* 0xd2 */ - i_rotshft_wcl, /* 0xd3 */ - i_aam, /* 0xd4 */ - i_aad, /* 0xd5 */ - i_setalc, - i_trans, /* 0xd7 */ - i_fpo, /* 0xd8 */ - i_fpo, /* 0xd9 */ - i_fpo, /* 0xda */ - i_fpo, /* 0xdb */ - i_fpo, /* 0xdc */ - i_fpo, /* 0xdd */ - i_fpo, /* 0xde */ - i_fpo, /* 0xdf */ - i_loopne, /* 0xe0 */ - i_loope, /* 0xe1 */ - i_loop, /* 0xe2 */ - i_jcxz, /* 0xe3 */ - i_inal, /* 0xe4 */ - i_inax, /* 0xe5 */ - i_outal, /* 0xe6 */ - i_outax, /* 0xe7 */ - i_call_d16, /* 0xe8 */ - i_jmp_d16, /* 0xe9 */ - i_jmp_far, /* 0xea */ - i_jmp_d8, /* 0xeb */ - i_inaldx, /* 0xec */ - i_inaxdx, /* 0xed */ - i_outdxal, /* 0xee */ - i_outdxax, /* 0xef */ - i_lock, /* 0xf0 */ - i_invalid, /* 0xf1 */ - i_repne, /* 0xf2 */ - i_repe, /* 0xf3 */ - i_hlt, /* 0xf4 */ - i_cmc, /* 0xf5 */ - i_f6pre, /* 0xf6 */ - i_f7pre, /* 0xf7 */ - i_clc, /* 0xf8 */ - i_stc, /* 0xf9 */ - i_di, /* 0xfa */ - i_ei, /* 0xfb */ - i_cld, /* 0xfc */ - i_std, /* 0xfd */ - i_fepre, /* 0xfe */ - i_ffpre /* 0xff */ -}; + { + i_add_br8, /* 0x00 */ + i_add_wr16, /* 0x01 */ + i_add_r8b, /* 0x02 */ + i_add_r16w, /* 0x03 */ + i_add_ald8, /* 0x04 */ + i_add_axd16, /* 0x05 */ + i_push_es, /* 0x06 */ + i_pop_es, /* 0x07 */ + i_or_br8, /* 0x08 */ + i_or_wr16, /* 0x09 */ + i_or_r8b, /* 0x0a */ + i_or_r16w, /* 0x0b */ + i_or_ald8, /* 0x0c */ + i_or_axd16, /* 0x0d */ + i_push_cs, /* 0x0e */ + i_pre_nec, /* 0x0f */ + i_adc_br8, /* 0x10 */ + i_adc_wr16, /* 0x11 */ + i_adc_r8b, /* 0x12 */ + i_adc_r16w, /* 0x13 */ + i_adc_ald8, /* 0x14 */ + i_adc_axd16, /* 0x15 */ + i_push_ss, /* 0x16 */ + i_pop_ss, /* 0x17 */ + i_sbb_br8, /* 0x18 */ + i_sbb_wr16, /* 0x19 */ + i_sbb_r8b, /* 0x1a */ + i_sbb_r16w, /* 0x1b */ + i_sbb_ald8, /* 0x1c */ + i_sbb_axd16, /* 0x1d */ + i_push_ds, /* 0x1e */ + i_pop_ds, /* 0x1f */ + i_and_br8, /* 0x20 */ + i_and_wr16, /* 0x21 */ + i_and_r8b, /* 0x22 */ + i_and_r16w, /* 0x23 */ + i_and_ald8, /* 0x24 */ + i_and_axd16, /* 0x25 */ + i_es, /* 0x26 */ + i_daa, /* 0x27 */ + i_sub_br8, /* 0x28 */ + i_sub_wr16, /* 0x29 */ + i_sub_r8b, /* 0x2a */ + i_sub_r16w, /* 0x2b */ + i_sub_ald8, /* 0x2c */ + i_sub_axd16, /* 0x2d */ + i_cs, /* 0x2e */ + i_das, /* 0x2f */ + i_xor_br8, /* 0x30 */ + i_xor_wr16, /* 0x31 */ + i_xor_r8b, /* 0x32 */ + i_xor_r16w, /* 0x33 */ + i_xor_ald8, /* 0x34 */ + i_xor_axd16, /* 0x35 */ + i_ss, /* 0x36 */ + i_aaa, /* 0x37 */ + i_cmp_br8, /* 0x38 */ + i_cmp_wr16, /* 0x39 */ + i_cmp_r8b, /* 0x3a */ + i_cmp_r16w, /* 0x3b */ + i_cmp_ald8, /* 0x3c */ + i_cmp_axd16, /* 0x3d */ + i_ds, /* 0x3e */ + i_aas, /* 0x3f */ + i_inc_ax, /* 0x40 */ + i_inc_cx, /* 0x41 */ + i_inc_dx, /* 0x42 */ + i_inc_bx, /* 0x43 */ + i_inc_sp, /* 0x44 */ + i_inc_bp, /* 0x45 */ + i_inc_si, /* 0x46 */ + i_inc_di, /* 0x47 */ + i_dec_ax, /* 0x48 */ + i_dec_cx, /* 0x49 */ + i_dec_dx, /* 0x4a */ + i_dec_bx, /* 0x4b */ + i_dec_sp, /* 0x4c */ + i_dec_bp, /* 0x4d */ + i_dec_si, /* 0x4e */ + i_dec_di, /* 0x4f */ + i_push_ax, /* 0x50 */ + i_push_cx, /* 0x51 */ + i_push_dx, /* 0x52 */ + i_push_bx, /* 0x53 */ + i_push_sp, /* 0x54 */ + i_push_bp, /* 0x55 */ + i_push_si, /* 0x56 */ + i_push_di, /* 0x57 */ + i_pop_ax, /* 0x58 */ + i_pop_cx, /* 0x59 */ + i_pop_dx, /* 0x5a */ + i_pop_bx, /* 0x5b */ + i_pop_sp, /* 0x5c */ + i_pop_bp, /* 0x5d */ + i_pop_si, /* 0x5e */ + i_pop_di, /* 0x5f */ + i_pusha, /* 0x60 */ + i_popa, /* 0x61 */ + i_chkind, /* 0x62 */ + i_invalid, /* 0x63 */ + i_repnc, /* 0x64 */ + i_repc, /* 0x65 */ + i_invalid, /* 0x66 */ + i_invalid, /* 0x67 */ + i_push_d16, /* 0x68 */ + i_imul_d16, /* 0x69 */ + i_push_d8, /* 0x6a */ + i_imul_d8, /* 0x6b */ + i_insb, /* 0x6c */ + i_insw, /* 0x6d */ + i_outsb, /* 0x6e */ + i_outsw, /* 0x6f */ + i_jo, /* 0x70 */ + i_jno, /* 0x71 */ + i_jc, /* 0x72 */ + i_jnc, /* 0x73 */ + i_jz, /* 0x74 */ + i_jnz, /* 0x75 */ + i_jce, /* 0x76 */ + i_jnce, /* 0x77 */ + i_js, /* 0x78 */ + i_jns, /* 0x79 */ + i_jp, /* 0x7a */ + i_jnp, /* 0x7b */ + i_jl, /* 0x7c */ + i_jnl, /* 0x7d */ + i_jle, /* 0x7e */ + i_jnle, /* 0x7f */ + i_80pre, /* 0x80 */ + i_81pre, /* 0x81 */ + i_82pre, /* 0x82 */ + i_83pre, /* 0x83 */ + i_test_br8, /* 0x84 */ + i_test_wr16, /* 0x85 */ + i_xchg_br8, /* 0x86 */ + i_xchg_wr16, /* 0x87 */ + i_mov_br8, /* 0x88 */ + i_mov_wr16, /* 0x89 */ + i_mov_r8b, /* 0x8a */ + i_mov_r16w, /* 0x8b */ + i_mov_wsreg, /* 0x8c */ + i_lea, /* 0x8d */ + i_mov_sregw, /* 0x8e */ + i_popw, /* 0x8f */ + i_nop, /* 0x90 */ + i_xchg_axcx, /* 0x91 */ + i_xchg_axdx, /* 0x92 */ + i_xchg_axbx, /* 0x93 */ + i_xchg_axsp, /* 0x94 */ + i_xchg_axbp, /* 0x95 */ + i_xchg_axsi, /* 0x97 */ + i_xchg_axdi, /* 0x97 */ + i_cbw, /* 0x98 */ + i_cwd, /* 0x99 */ + i_call_far, /* 0x9a */ + i_wait, /* 0x9b */ + i_pushf, /* 0x9c */ + i_popf, /* 0x9d */ + i_sahf, /* 0x9e */ + i_lahf, /* 0x9f */ + i_mov_aldisp, /* 0xa0 */ + i_mov_axdisp, /* 0xa1 */ + i_mov_dispal, /* 0xa2 */ + i_mov_dispax, /* 0xa3 */ + i_movsb, /* 0xa4 */ + i_movsw, /* 0xa5 */ + i_cmpsb, /* 0xa6 */ + i_cmpsw, /* 0xa7 */ + i_test_ald8, /* 0xa8 */ + i_test_axd16, /* 0xa9 */ + i_stosb, /* 0xaa */ + i_stosw, /* 0xab */ + i_lodsb, /* 0xac */ + i_lodsw, /* 0xad */ + i_scasb, /* 0xae */ + i_scasw, /* 0xaf */ + i_mov_ald8, /* 0xb0 */ + i_mov_cld8, /* 0xb1 */ + i_mov_dld8, /* 0xb2 */ + i_mov_bld8, /* 0xb3 */ + i_mov_ahd8, /* 0xb4 */ + i_mov_chd8, /* 0xb5 */ + i_mov_dhd8, /* 0xb6 */ + i_mov_bhd8, /* 0xb7 */ + i_mov_axd16, /* 0xb8 */ + i_mov_cxd16, /* 0xb9 */ + i_mov_dxd16, /* 0xba */ + i_mov_bxd16, /* 0xbb */ + i_mov_spd16, /* 0xbc */ + i_mov_bpd16, /* 0xbd */ + i_mov_sid16, /* 0xbe */ + i_mov_did16, /* 0xbf */ + i_rotshft_bd8, /* 0xc0 */ + i_rotshft_wd8, /* 0xc1 */ + i_ret_d16, /* 0xc2 */ + i_ret, /* 0xc3 */ + i_les_dw, /* 0xc4 */ + i_lds_dw, /* 0xc5 */ + i_mov_bd8, /* 0xc6 */ + i_mov_wd16, /* 0xc7 */ + i_enter, /* 0xc8 */ + i_leave, /* 0xc9 */ + i_retf_d16, /* 0xca */ + i_retf, /* 0xcb */ + i_int3, /* 0xcc */ + i_int, /* 0xcd */ + i_into, /* 0xce */ + i_iret, /* 0xcf */ + i_rotshft_b, /* 0xd0 */ + i_rotshft_w, /* 0xd1 */ + i_rotshft_bcl, /* 0xd2 */ + i_rotshft_wcl, /* 0xd3 */ + i_aam, /* 0xd4 */ + i_aad, /* 0xd5 */ + i_setalc, /* 0xd6 */ + i_trans, /* 0xd7 */ + i_fpo, /* 0xd8 */ + i_fpo, /* 0xd9 */ + i_fpo, /* 0xda */ + i_fpo, /* 0xdb */ + i_fpo, /* 0xdc */ + i_fpo, /* 0xdd */ + i_fpo, /* 0xde */ + i_fpo, /* 0xdf */ + i_loopne, /* 0xe0 */ + i_loope, /* 0xe1 */ + i_loop, /* 0xe2 */ + i_jcxz, /* 0xe3 */ + i_inal, /* 0xe4 */ + i_inax, /* 0xe5 */ + i_outal, /* 0xe6 */ + i_outax, /* 0xe7 */ + i_call_d16, /* 0xe8 */ + i_jmp_d16, /* 0xe9 */ + i_jmp_far, /* 0xea */ + i_jmp_d8, /* 0xeb */ + i_inaldx, /* 0xec */ + i_inaxdx, /* 0xed */ + i_outdxal, /* 0xee */ + i_outdxax, /* 0xef */ + i_lock, /* 0xf0 */ + i_invalid, /* 0xf1 */ + i_repne, /* 0xf2 */ + i_repe, /* 0xf3 */ + i_hlt, /* 0xf4 */ + i_cmc, /* 0xf5 */ + i_f6pre, /* 0xf6 */ + i_f7pre, /* 0xf7 */ + i_clc, /* 0xf8 */ + i_stc, /* 0xf9 */ + i_di, /* 0xfa */ + i_ei, /* 0xfb */ + i_cld, /* 0xfc */ + i_std, /* 0xfd */ + i_fepre, /* 0xfe */ + i_ffpre /* 0xff */ + }; diff --git a/source/nec/necintrf.h b/source/nec/necintrf.h index 6ac996c..57ee49c 100644 --- a/source/nec/necintrf.h +++ b/source/nec/necintrf.h @@ -1,3 +1,10 @@ +/* + * NewOswan + * necintrf.h: + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ /* ASG 971222 -- rewrote this interface */ #ifndef __NECITRF_H_ #define __NECITRF_H_ @@ -6,65 +13,35 @@ enum { - NEC_IP=1, NEC_AW, NEC_CW, NEC_DW, NEC_BW, NEC_SP, NEC_BP, NEC_IX, NEC_IY, - NEC_FLAGS, NEC_ES, NEC_CS, NEC_SS, NEC_DS, - NEC_VECTOR, NEC_PENDING, NEC_NMI_STATE, NEC_IRQ_STATE + NEC_IP = 1, + NEC_AW, + NEC_CW, + NEC_DW, + NEC_BW, + NEC_SP, + NEC_BP, + NEC_IX, + NEC_IY, + NEC_FLAGS, + NEC_ES, + NEC_CS, + NEC_SS, + NEC_DS, + NEC_VECTOR, + NEC_PENDING, + NEC_NMI_STATE, + NEC_IRQ_STATE }; /* Public variables */ extern int nec_ICount; /* Public functions */ - -/* -#define v20_ICount nec_ICount -extern void v20_init(void); -extern void v20_reset(void *param); -extern void v20_exit(void); -extern int v20_execute(int cycles); -extern unsigned v20_get_context(void *dst); -extern void v20_set_context(void *src); -extern unsigned v20_get_reg(int regnum); -extern void v20_set_reg(int regnum, unsigned val); -extern void v20_set_irq_line(int irqline, int state); -extern void v20_set_irq_callback(int (*callback)(int irqline)); -extern const char *v20_info(void *context, int regnum); -extern unsigned v20_dasm(char *buffer, unsigned pc); - -#define v30_ICount nec_ICount -extern void v30_init(void); -extern void v30_reset(void *param); -extern void v30_exit(void); -extern int v30_execute(int cycles); -extern unsigned v30_get_context(void *dst); -extern void v30_set_context(void *src); -extern unsigned v30_get_reg(int regnum); -extern void v30_set_reg(int regnum, unsigned val); -extern void v30_set_irq_line(int irqline, int state); -extern void v30_set_irq_callback(int (*callback)(int irqline)); -extern const char *v30_info(void *context, int regnum); -extern unsigned v30_dasm(char *buffer, unsigned pc); - -#define v33_ICount nec_ICount -extern void v33_init(void); -extern void v33_reset(void *param); -extern void v33_exit(void); -extern int v33_execute(int cycles); -extern unsigned v33_get_context(void *dst); -extern void v33_set_context(void *src); -extern unsigned v33_get_reg(int regnum); -extern void v33_set_reg(int regnum, unsigned val); -extern void v33_set_irq_line(int irqline, int state); -extern void v33_set_irq_callback(int (*callback)(int irqline)); -extern const char *v33_info(void *context, int regnum); -extern unsigned v33_dasm(char *buffer, unsigned pc); -*/ - void nec_set_irq_line(int irqline, int state); void nec_set_reg(int regnum, uint32_t val); int nec_execute(int cycles); unsigned nec_get_reg(int regnum); -void nec_reset (void *param); +void nec_reset(void *param); void nec_int(uint16_t vector); uint8_t cpu_readport(uint8_t); diff --git a/source/nec/necmodrm.h b/source/nec/necmodrm.h index 3236ab3..2e1a46b 100644 --- a/source/nec/necmodrm.h +++ b/source/nec/necmodrm.h @@ -1,107 +1,115 @@ +/* + * NewOswan + * necmodrm.h: + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ + static struct { - struct - { - WREGS w[256]; - BREGS b[256]; - } reg; - struct - { - WREGS w[256]; - BREGS b[256]; - } RM; + struct + { + WREGS w[256]; + BREGS b[256]; + } reg; + struct + { + WREGS w[256]; + BREGS b[256]; + } RM; } Mod_RM; #define RegWord(ModRM) I.regs.w[Mod_RM.reg.w[ModRM]] #define RegByte(ModRM) I.regs.b[Mod_RM.reg.b[ModRM]] #define GetRMWord(ModRM) \ - ((ModRM) >= 0xc0 ? I.regs.w[Mod_RM.RM.w[ModRM]] : ( (*GetEA[ModRM])(), ReadWord( EA ) )) + ((ModRM) >= 0xc0 ? I.regs.w[Mod_RM.RM.w[ModRM]] : ( (*GetEA[ModRM])(), ReadWord( EA ) )) -#define PutbackRMWord(ModRM,val) \ -{ \ - if (ModRM >= 0xc0) I.regs.w[Mod_RM.RM.w[ModRM]]=val; \ +#define PutbackRMWord(ModRM, val) \ +{ \ + if (ModRM >= 0xc0) I.regs.w[Mod_RM.RM.w[ModRM]]=val; \ else WriteWord(EA,val); \ } #define GetnextRMWord ReadWord((EA&0xf0000)|((EA+2)&0xffff)) -#define PutRMWord(ModRM,val) \ -{ \ - if (ModRM >= 0xc0) \ - I.regs.w[Mod_RM.RM.w[ModRM]]=val; \ - else { \ - (*GetEA[ModRM])(); \ - WriteWord( EA ,val); \ - } \ +#define PutRMWord(ModRM, val) \ +{ \ + if (ModRM >= 0xc0) \ + I.regs.w[Mod_RM.RM.w[ModRM]]=val; \ + else { \ + (*GetEA[ModRM])(); \ + WriteWord( EA ,val); \ + } \ } -#define PutImmRMWord(ModRM) \ -{ \ - int16_t val; \ - if (ModRM >= 0xc0) \ - FETCHWORD(I.regs.w[Mod_RM.RM.w[ModRM]]) \ - else { \ - (*GetEA[ModRM])(); \ - FETCHWORD(val) \ - WriteWord( EA , val); \ - } \ +#define PutImmRMWord(ModRM) \ +{ \ + int16_t val; \ + if (ModRM >= 0xc0) \ + FETCHWORD(I.regs.w[Mod_RM.RM.w[ModRM]]) \ + else { \ + (*GetEA[ModRM])(); \ + FETCHWORD(val) \ + WriteWord( EA , val); \ + } \ } #define GetRMByte(ModRM) \ - ((ModRM) >= 0xc0 ? I.regs.b[Mod_RM.RM.b[ModRM]] : ReadByte( (*GetEA[ModRM])() )) + ((ModRM) >= 0xc0 ? I.regs.b[Mod_RM.RM.b[ModRM]] : ReadByte( (*GetEA[ModRM])() )) -#define PutRMByte(ModRM,val) \ -{ \ - if (ModRM >= 0xc0) \ - I.regs.b[Mod_RM.RM.b[ModRM]]=val; \ - else \ - WriteByte( (*GetEA[ModRM])() ,val); \ +#define PutRMByte(ModRM, val) \ +{ \ + if (ModRM >= 0xc0) \ + I.regs.b[Mod_RM.RM.b[ModRM]]=val; \ + else \ + WriteByte( (*GetEA[ModRM])() ,val); \ } -#define PutImmRMByte(ModRM) \ -{ \ - if (ModRM >= 0xc0) \ - I.regs.b[Mod_RM.RM.b[ModRM]]=FETCH; \ - else { \ - (*GetEA[ModRM])(); \ - WriteByte( EA , FETCH ); \ - } \ +#define PutImmRMByte(ModRM) \ +{ \ + if (ModRM >= 0xc0) \ + I.regs.b[Mod_RM.RM.b[ModRM]]=FETCH; \ + else { \ + (*GetEA[ModRM])(); \ + WriteByte( EA , FETCH ); \ + } \ } -#define PutbackRMByte(ModRM,val) \ -{ \ - if (ModRM >= 0xc0) \ - I.regs.b[Mod_RM.RM.b[ModRM]]=val; \ - else \ - WriteByte(EA,val); \ +#define PutbackRMByte(ModRM, val) \ +{ \ + if (ModRM >= 0xc0) \ + I.regs.b[Mod_RM.RM.b[ModRM]]=val; \ + else \ + WriteByte(EA,val); \ } -#define DEF_br8 \ - uint32_t ModRM = FETCH,src,dst; \ - src = RegByte(ModRM); \ +#define DEF_br8 \ + uint32_t ModRM = FETCH,src,dst; \ + src = RegByte(ModRM); \ dst = GetRMByte(ModRM) -#define DEF_wr16 \ - uint32_t ModRM = FETCH,src,dst; \ - src = RegWord(ModRM); \ +#define DEF_wr16 \ + uint32_t ModRM = FETCH,src,dst; \ + src = RegWord(ModRM); \ dst = GetRMWord(ModRM) -#define DEF_r8b \ - uint32_t ModRM = FETCH,src,dst; \ - dst = RegByte(ModRM); \ +#define DEF_r8b \ + uint32_t ModRM = FETCH,src,dst; \ + dst = RegByte(ModRM); \ src = GetRMByte(ModRM) -#define DEF_r16w \ - uint32_t ModRM = FETCH,src,dst; \ - dst = RegWord(ModRM); \ +#define DEF_r16w \ + uint32_t ModRM = FETCH,src,dst; \ + dst = RegWord(ModRM); \ src = GetRMWord(ModRM) -#define DEF_ald8 \ - uint32_t src = FETCH; \ - uint32_t dst = I.regs.b[AL] +#define DEF_ald8 \ + uint32_t src = FETCH; \ + uint32_t dst = I.regs.b[AL] -#define DEF_axd16 \ - uint32_t src = FETCH; \ - uint32_t dst = I.regs.w[AW]; \ +#define DEF_axd16 \ + uint32_t src = FETCH; \ + uint32_t dst = I.regs.w[AW]; \ src += (FETCH << 8) diff --git a/source/rom.c b/source/rom.c index 16d20fb..f93e637 100644 --- a/source/rom.c +++ b/source/rom.c @@ -1,5 +1,10 @@ -////////////////////////////////////////////////////////////////////////////// -// +/* + * NewOswan + * tom.c: + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ ////////////////////////////////////////////////////////////////////////////// // // @@ -21,7 +26,6 @@ #include "log.h" #include "rom.h" - //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -33,7 +37,7 @@ // // //////////////////////////////////////////////////////////////////////////////// -uint8_t *ws_rom_load(char *path, uint32_t *romSize) +uint8_t *ws_rom_load(char *path, uint32_t *romSize) { int fd; uint8_t *ret_ptr; @@ -45,7 +49,7 @@ uint8_t *ws_rom_load(char *path, uint32_t *romSize) *romSize = FileStat.st_size; - ret_ptr = (uint8_t *)mmap(NULL, FileStat.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + ret_ptr = (uint8_t *)mmap(NULL, FileStat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); close(fd); @@ -57,6 +61,7 @@ uint8_t *ws_rom_load(char *path, uint32_t *romSize) return ret_ptr; } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -68,68 +73,69 @@ uint8_t *ws_rom_load(char *path, uint32_t *romSize) // // //////////////////////////////////////////////////////////////////////////////// -void ws_rom_dumpInfo(uint8_t *wsrom, uint32_t romSize) +void ws_rom_dumpInfo(uint8_t *wsrom, uint32_t romSize) { - ws_romHeaderStruct *romHeader=ws_rom_getHeader(wsrom,romSize); + ws_romHeaderStruct *romHeader = ws_rom_getHeader(wsrom, romSize); - fprintf(log_get(),"rom: developper Id 0x%.2x\n",romHeader->developperId); - fprintf(log_get(),"rom: cart Id 0x%.2x\n",romHeader->cartId); - fprintf(log_get(),"rom: minimum system %s\n",(romHeader->minimumSupportSystem==0)?"Wonderswan mono":"Wonderswan color"); - fprintf(log_get(),"rom: size %i Mbits\n",(romSize>>20)<<3); - fprintf(log_get(),"rom: eeprom "); + fprintf(log_get(), "rom: developper Id 0x%.2x\n", romHeader->developperId); + fprintf(log_get(), "rom: cart Id 0x%.2x\n", romHeader->cartId); + fprintf(log_get(), "rom: minimum system %s\n", + (romHeader->minimumSupportSystem == 0) ? "Wonderswan mono" : "Wonderswan color"); + fprintf(log_get(), "rom: size %i Mbits\n", (romSize >> 20) << 3); + fprintf(log_get(), "rom: eeprom "); - switch (romHeader->eepromSize&0xf) - { - case WS_EEPROM_SIZE_NONE: - { - fprintf(log_get(),"none\n"); - break; - } + switch (romHeader->eepromSize & 0xf) + { + case WS_EEPROM_SIZE_NONE: + { + fprintf(log_get(), "none\n"); + break; + } - case WS_EEPROM_SIZE_64k: - { - fprintf(log_get(),"64 kb\n"); - break; - } + case WS_EEPROM_SIZE_64k: + { + fprintf(log_get(), "64 kb\n"); + break; + } - case WS_EEPROM_SIZE_256k: - { - fprintf(log_get(),"256 kb\n"); - break; - } - } + case WS_EEPROM_SIZE_256k: + { + fprintf(log_get(), "256 kb\n"); + break; + } + } - fprintf(log_get(),"rom: sram "); + fprintf(log_get(), "rom: sram "); - switch (romHeader->eepromSize&0xf0) - { - case WS_SRAM_SIZE_NONE: - { - fprintf(log_get(),"none\n"); - break; - } + switch (romHeader->eepromSize & 0xf0) + { + case WS_SRAM_SIZE_NONE: + { + fprintf(log_get(), "none\n"); + break; + } - case WS_SRAM_SIZE_1k: - { - fprintf(log_get(),"1 kb\n"); - break; - } + case WS_SRAM_SIZE_1k: + { + fprintf(log_get(), "1 kb\n"); + break; + } - case WS_SRAM_SIZE_16k: - { - fprintf(log_get(),"16 kb\n"); - break; - } + case WS_SRAM_SIZE_16k: + { + fprintf(log_get(), "16 kb\n"); + break; + } - case WS_SRAM_SIZE_8k: - { - fprintf(log_get(),"8 kn\n"); - break; - } - } + case WS_SRAM_SIZE_8k: + { + fprintf(log_get(), "8 kn\n"); + break; + } + } - fprintf(log_get(),"rom: rtc %s\n",(romHeader->realtimeClock)?"Yes":"None"); - fprintf(log_get(),"checksum 0x%.4x\n",romHeader->checksum); + fprintf(log_get(), "rom: rtc %s\n", (romHeader->realtimeClock) ? "Yes" : "None"); + fprintf(log_get(), "checksum 0x%.4x\n", romHeader->checksum); } @@ -144,12 +150,13 @@ void ws_rom_dumpInfo(uint8_t *wsrom, uint32_t romSize) // // //////////////////////////////////////////////////////////////////////////////// -ws_romHeaderStruct *ws_rom_getHeader(uint8_t *wsrom, uint32_t wsromSize) +ws_romHeaderStruct *ws_rom_getHeader(uint8_t *wsrom, uint32_t wsromSize) { ws_romHeaderStruct *wsromHeader = (ws_romHeaderStruct *)(wsrom + wsromSize - 10); - return(wsromHeader); + return (wsromHeader); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -161,27 +168,28 @@ ws_romHeaderStruct *ws_rom_getHeader(uint8_t *wsrom, uint32_t wsromSize) // // //////////////////////////////////////////////////////////////////////////////// -uint32_t ws_rom_sramSize(uint8_t *wsrom, uint32_t wsromSize) +uint32_t ws_rom_sramSize(uint8_t *wsrom, uint32_t wsromSize) { - ws_romHeaderStruct *romHeader=ws_rom_getHeader(wsrom,wsromSize); + ws_romHeaderStruct *romHeader = ws_rom_getHeader(wsrom, wsromSize); - switch (romHeader->eepromSize&0xf0) - { - case WS_SRAM_SIZE_NONE: - return(0); + switch (romHeader->eepromSize & 0xf0) + { + case WS_SRAM_SIZE_NONE: + return (0); - case WS_SRAM_SIZE_1k: - return(0x400); + case WS_SRAM_SIZE_1k: + return (0x400); - case WS_SRAM_SIZE_16k: - return(0x4000); + case WS_SRAM_SIZE_16k: + return (0x4000); - case WS_SRAM_SIZE_8k: - return(0x2000); - } + case WS_SRAM_SIZE_8k: + return (0x2000); + } - return(0); + return (0); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -193,22 +201,22 @@ uint32_t ws_rom_sramSize(uint8_t *wsrom, uint32_t wsromSize) // // //////////////////////////////////////////////////////////////////////////////// -uint32_t ws_rom_eepromSize(uint8_t *wsrom, uint32_t wsromSize) +uint32_t ws_rom_eepromSize(uint8_t *wsrom, uint32_t wsromSize) { - ws_romHeaderStruct *romHeader=ws_rom_getHeader(wsrom,wsromSize); + ws_romHeaderStruct *romHeader = ws_rom_getHeader(wsrom, wsromSize); - switch (romHeader->eepromSize&0xf) - { - case WS_EEPROM_SIZE_NONE: - return(0); + switch (romHeader->eepromSize & 0xf) + { + case WS_EEPROM_SIZE_NONE: + return (0); - case WS_EEPROM_SIZE_64k: - return(0x10000); + case WS_EEPROM_SIZE_64k: + return (0x10000); - case WS_EEPROM_SIZE_256k: - return(0x40000); - } + case WS_EEPROM_SIZE_256k: + return (0x40000); + } - return(0); + return (0); } diff --git a/source/rom.h b/source/rom.h index d394444..944ed2d 100644 --- a/source/rom.h +++ b/source/rom.h @@ -1,5 +1,10 @@ -////////////////////////////////////////////////////////////////////////////// -// +/* + * NewOswan + * rom.h: + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ ////////////////////////////////////////////////////////////////////////////// // // @@ -14,43 +19,40 @@ #include -#define WS_ROM_SIZE_2MBIT 1 -#define WS_ROM_SIZE_4MBIT 2 -#define WS_ROM_SIZE_8MBIT 3 -#define WS_ROM_SIZE_16MBIT 4 -#define WS_ROM_SIZE_24MBIT 5 -#define WS_ROM_SIZE_32MBIT 6 -#define WS_ROM_SIZE_48MBIT 7 -#define WS_ROM_SIZE_64MBIT 8 -#define WS_ROM_SIZE_128MBIT 9 - -#define WS_EEPROM_SIZE_NONE 0 -#define WS_SRAM_SIZE_NONE 0 -#define WS_EEPROM_SIZE_64k 1 -#define WS_EEPROM_SIZE_256k 2 -#define WS_SRAM_SIZE_1k 10 -#define WS_SRAM_SIZE_16k 20 -#define WS_SRAM_SIZE_8k 50 +#define WS_ROM_SIZE_2MBIT 1 +#define WS_ROM_SIZE_4MBIT 2 +#define WS_ROM_SIZE_8MBIT 3 +#define WS_ROM_SIZE_16MBIT 4 +#define WS_ROM_SIZE_24MBIT 5 +#define WS_ROM_SIZE_32MBIT 6 +#define WS_ROM_SIZE_48MBIT 7 +#define WS_ROM_SIZE_64MBIT 8 +#define WS_ROM_SIZE_128MBIT 9 +#define WS_EEPROM_SIZE_NONE 0 +#define WS_SRAM_SIZE_NONE 0 +#define WS_EEPROM_SIZE_64k 1 +#define WS_EEPROM_SIZE_256k 2 +#define WS_SRAM_SIZE_1k 10 +#define WS_SRAM_SIZE_16k 20 +#define WS_SRAM_SIZE_8k 50 typedef struct ws_romHeaderStruct { - uint8_t developperId; - uint8_t minimumSupportSystem; - uint8_t cartId; - uint8_t romSize; - uint8_t eepromSize; - uint8_t additionnalCapabilities; - uint8_t realtimeClock; - uint16_t checksum; + uint8_t developperId; + uint8_t minimumSupportSystem; + uint8_t cartId; + uint8_t romSize; + uint8_t eepromSize; + uint8_t additionnalCapabilities; + uint8_t realtimeClock; + uint16_t checksum; } ws_romHeaderStruct; - -uint8_t *ws_rom_load(char *path, uint32_t *romSize); -void ws_rom_dumpInfo(uint8_t *wsrom, uint32_t wsromSize); -ws_romHeaderStruct *ws_rom_getHeader(uint8_t *wsrom, uint32_t wsromSize); -uint32_t ws_rom_sramSize(uint8_t *wsrom, uint32_t wsromSize); -uint32_t ws_rom_eepromSize(uint8_t *wsrom, uint32_t wsromSize); +uint8_t *ws_rom_load(char *path, uint32_t *romSize); +void ws_rom_dumpInfo(uint8_t *wsrom, uint32_t wsromSize); +ws_romHeaderStruct *ws_rom_getHeader(uint8_t *wsrom, uint32_t wsromSize); +uint32_t ws_rom_sramSize(uint8_t *wsrom, uint32_t wsromSize); +uint32_t ws_rom_eepromSize(uint8_t *wsrom, uint32_t wsromSize); #endif - diff --git a/source/ws.c b/source/ws.c index dadeb9f..1b1aecc 100644 --- a/source/ws.c +++ b/source/ws.c @@ -1,5 +1,10 @@ -//////////////////////////////////////////////////////////////////////////////// -// Wonderswan emulator +/* + * NewOswan + * ws.c: Base wonderswan implementation + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ //////////////////////////////////////////////////////////////////////////////// // // @@ -43,14 +48,14 @@ // //////////////////////////////////////////////////////////////////////////////// -uint32_t ws_cycles; -uint32_t ws_skip; -uint32_t ws_cyclesByLine=0; -uint32_t vblank_count=0; +uint32_t ws_cycles; +uint32_t ws_skip; +uint32_t ws_cyclesByLine = 0; +uint32_t vblank_count = 0; char *ws_sram_path = NULL; char *ws_ieep_path = NULL; -char *ws_rom_path = NULL; +char *ws_rom_path = NULL; wssystem_t systemType; //////////////////////////////////////////////////////////////////////////////// @@ -67,15 +72,15 @@ wssystem_t systemType; void ws_patchRom(void) { - uint8_t *rom=memory_getRom(); - uint32_t romSize=memory_getRomSize(); + uint8_t *rom = memory_getRom(); + uint32_t romSize = memory_getRomSize(); - fprintf(log_get(),"developper Id: 0x%.2x\nGame Id: 0x%.2x\n",rom[romSize-10],rom[romSize-8]); + fprintf(log_get(), "developper Id: 0x%.2x\nGame Id: 0x%.2x\n", rom[romSize - 10], rom[romSize - 8]); - if (!ws_cyclesByLine) - { - ws_cyclesByLine=256; - } + if (!ws_cyclesByLine) + { + ws_cyclesByLine = 256; + } } //////////////////////////////////////////////////////////////////////////////// @@ -91,53 +96,53 @@ void ws_patchRom(void) //////////////////////////////////////////////////////////////////////////////// int ws_init(char *rompath) { - uint8_t *rom; - uint32_t romSize; + uint8_t *rom; + uint32_t romSize; - if ((rom=ws_rom_load(rompath,&romSize))==NULL) - { - printf("Error: cannot load %s\n",rompath); - return(0); - } + if ((rom = ws_rom_load(rompath, &romSize)) == NULL) + { + printf("Error: cannot load %s\n", rompath); + return (0); + } + ws_memory_init(rom, romSize); + ws_patchRom(); - ws_memory_init(rom, romSize); - ws_patchRom(); + ws_staticRam = (uint8_t *)load_file(ws_ieep_path); + if (ws_staticRam == NULL) + { + ws_staticRam = (uint8_t *)create_file(ws_sram_path, 0x10000); + } - ws_staticRam = (uint8_t *)load_file(ws_ieep_path); - if (ws_staticRam == NULL) - { - ws_staticRam = (uint8_t *)create_file(ws_sram_path, 0x10000); - } + if (ws_staticRam == NULL) + { + printf("Card SRAM load error!\n"); + return 0; + } - if (ws_staticRam == NULL) - { - printf("Card SRAM load error!\n"); - return 0; - } + externalEeprom = (uint8_t *)load_file(ws_ieep_path); + if (externalEeprom == NULL) + { + externalEeprom = (uint8_t *)create_file(ws_ieep_path, 0x100000); + } + if (externalEeprom == NULL) + { + printf("Card EEPROM load error!\n"); + return 0; + } - externalEeprom = (uint8_t *)load_file(ws_ieep_path); - if (externalEeprom == NULL) - { - externalEeprom = (uint8_t *)create_file(ws_ieep_path, 0x100000); - } - if (externalEeprom == NULL) - { - printf("Card EEPROM load error!\n"); - return 0; - } + ws_io_init(); + ws_audio_init(); + ws_gpu_init(); - ws_io_init(); - ws_audio_init(); - ws_gpu_init(); + if (ws_rotated()) + { + ws_io_flipControls(); + } - if (ws_rotated()) - { - ws_io_flipControls(); - } - - return(1); + return (1); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -151,13 +156,14 @@ int ws_init(char *rompath) //////////////////////////////////////////////////////////////////////////////// void ws_reset(void) { - ws_memory_reset(); - ws_io_reset(); - ws_audio_reset(); - ws_gpu_reset(); - nec_reset(NULL); - nec_set_reg(NEC_SP,0x2000); + ws_memory_reset(); + ws_io_reset(); + ws_audio_reset(); + ws_gpu_reset(); + nec_reset(NULL); + nec_set_reg(NEC_SP, 0x2000); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -171,100 +177,101 @@ void ws_reset(void) //////////////////////////////////////////////////////////////////////////////// int ws_executeLine(int16_t *framebuffer, int renderLine) { - int drawWholeScreen=0; + int drawWholeScreen = 0; - ws_audio_process(); + ws_audio_process(); - // update scanline register - ws_ioRam[2]=ws_gpu_scanline; + // update scanline register + ws_ioRam[2] = ws_gpu_scanline; - /* Why twice like that and random cycle count???? */ - ws_cycles=nec_execute((ws_cyclesByLine>>1)+(rand()&7)); - ws_cycles+=nec_execute((ws_cyclesByLine>>1)+(rand()&7)); + /* Why twice like that and random cycle count???? */ + ws_cycles = nec_execute((ws_cyclesByLine >> 1) + (rand() & 7)); + ws_cycles += nec_execute((ws_cyclesByLine >> 1) + (rand() & 7)); - if(ws_cycles>=ws_cyclesByLine+ws_cyclesByLine) - { - ws_skip=ws_cycles/ws_cyclesByLine; - } - else - { - ws_skip=1; - } + if (ws_cycles >= ws_cyclesByLine + ws_cyclesByLine) + { + ws_skip = ws_cycles / ws_cyclesByLine; + } + else + { + ws_skip = 1; + } - ws_cycles%=ws_cyclesByLine; + ws_cycles %= ws_cyclesByLine; - for(uint32_t uI=0; uI158) - { - ws_gpu_scanline=0; - { - if((ws_ioRam[0xb2]&32)) /* VBLANK Timer */ - { - /* TODO: REPAIR THAT SHIT */ - ws_ioRam[0xb6]&=~32; - nec_int((ws_ioRam[0xb0]+5)*4); - } - } - } + if (ws_gpu_scanline > 158) + { + ws_gpu_scanline = 0; + { + if ((ws_ioRam[0xb2] & 32)) /* VBLANK Timer */ + { + /* TODO: REPAIR THAT SHIT */ + ws_ioRam[0xb6] &= ~32; + nec_int((ws_ioRam[0xb0] + 5) * 4); + } + } + } - ws_ioRam[2]=ws_gpu_scanline; + ws_ioRam[2] = ws_gpu_scanline; - if(drawWholeScreen) - { + if (drawWholeScreen) + { - if(ws_ioRam[0xb2]&64) /* VBLANK INT */ - { - ws_ioRam[0xb6]&=~64; - nec_int((ws_ioRam[0xb0]+6)*4); - } + if (ws_ioRam[0xb2] & 64) /* VBLANK INT */ + { + ws_ioRam[0xb6] &= ~64; + nec_int((ws_ioRam[0xb0] + 6) * 4); + } - vblank_count++; - } + vblank_count++; + } - if(ws_ioRam[0xa4]&&(ws_ioRam[0xb2]&128)) /*HBLANK TMR*/ - { - /* TODO: Check that shit */ - if(!ws_ioRam[0xa5]) - { - ws_ioRam[0xa5]=ws_ioRam[0xa4]; - } + if (ws_ioRam[0xa4] && (ws_ioRam[0xb2] & 128)) /*HBLANK TMR*/ + { + /* TODO: Check that shit */ + if (!ws_ioRam[0xa5]) + { + ws_ioRam[0xa5] = ws_ioRam[0xa4]; + } - if(ws_ioRam[0xa5]) - { - ws_ioRam[0xa5]--; - } + if (ws_ioRam[0xa5]) + { + ws_ioRam[0xa5]--; + } - if((!ws_ioRam[0xa5])&&(ws_ioRam[0xb2]&128)) - { + if ((!ws_ioRam[0xa5]) && (ws_ioRam[0xb2] & 128)) + { - ws_ioRam[0xb6]&=~128; - nec_int((ws_ioRam[0xb0]+7)*4); - } - } + ws_ioRam[0xb6] &= ~128; + nec_int((ws_ioRam[0xb0] + 7) * 4); + } + } - if((ws_ioRam[0x2]==ws_ioRam[0x3])&&(ws_ioRam[0xb2]&16)) /*SCANLINE INT*/ - { - ws_ioRam[0xb6]&=~16; - nec_int((ws_ioRam[0xb0]+4)*4); - } + if ((ws_ioRam[0x2] == ws_ioRam[0x3]) && (ws_ioRam[0xb2] & 16)) /*SCANLINE INT*/ + { + ws_ioRam[0xb6] &= ~16; + nec_int((ws_ioRam[0xb0] + 4) * 4); + } - return(drawWholeScreen); + return (drawWholeScreen); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -278,11 +285,12 @@ int ws_executeLine(int16_t *framebuffer, int renderLine) //////////////////////////////////////////////////////////////////////////////// void ws_done(void) { - ws_memory_done(); - ws_io_done(); - ws_audio_done(); - ws_gpu_done(); + ws_memory_done(); + ws_io_done(); + ws_audio_done(); + ws_gpu_done(); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -318,67 +326,67 @@ wssystem_t ws_get_system() // // //////////////////////////////////////////////////////////////////////////////// -#define MacroLoadNecRegisterFromFile(F,R) \ - read(fp,&value,sizeof(value)); \ - nec_set_reg(R,value); +#define MacroLoadNecRegisterFromFile(F, R) \ + read(fp,&value,sizeof(value)); \ + nec_set_reg(R,value); -int ws_loadState(char *statepath) +int ws_loadState(char *statepath) { - fprintf(log_get(),"loading %s\n",statepath); - uint16_t crc=memory_getRomCrc(); - uint16_t newCrc; - unsigned value; - uint8_t ws_newVideoMode; + fprintf(log_get(), "loading %s\n", statepath); + uint16_t crc = memory_getRomCrc(); + uint16_t newCrc; + unsigned value; + uint8_t ws_newVideoMode; - int fp = open(statepath, O_RDONLY); + int fp = open(statepath, O_RDONLY); - if (fp == -1) - { - return(0); - } + if (fp == -1) + { + return (0); + } - read(fp, &newCrc, 2); + read(fp, &newCrc, 2); - if (newCrc!=crc) - { - return(-1); - } + if (newCrc != crc) + { + return (-1); + } - MacroLoadNecRegisterFromFile(fp,NEC_IP); - MacroLoadNecRegisterFromFile(fp,NEC_AW); - MacroLoadNecRegisterFromFile(fp,NEC_BW); - MacroLoadNecRegisterFromFile(fp,NEC_CW); - MacroLoadNecRegisterFromFile(fp,NEC_DW); - MacroLoadNecRegisterFromFile(fp,NEC_CS); - MacroLoadNecRegisterFromFile(fp,NEC_DS); - MacroLoadNecRegisterFromFile(fp,NEC_ES); - MacroLoadNecRegisterFromFile(fp,NEC_SS); - MacroLoadNecRegisterFromFile(fp,NEC_IX); - MacroLoadNecRegisterFromFile(fp,NEC_IY); - MacroLoadNecRegisterFromFile(fp,NEC_BP); - MacroLoadNecRegisterFromFile(fp,NEC_SP); - MacroLoadNecRegisterFromFile(fp,NEC_FLAGS); - MacroLoadNecRegisterFromFile(fp,NEC_VECTOR); - MacroLoadNecRegisterFromFile(fp,NEC_PENDING); - MacroLoadNecRegisterFromFile(fp,NEC_NMI_STATE); - MacroLoadNecRegisterFromFile(fp,NEC_IRQ_STATE); + MacroLoadNecRegisterFromFile(fp, NEC_IP); + MacroLoadNecRegisterFromFile(fp, NEC_AW); + MacroLoadNecRegisterFromFile(fp, NEC_BW); + MacroLoadNecRegisterFromFile(fp, NEC_CW); + MacroLoadNecRegisterFromFile(fp, NEC_DW); + MacroLoadNecRegisterFromFile(fp, NEC_CS); + MacroLoadNecRegisterFromFile(fp, NEC_DS); + MacroLoadNecRegisterFromFile(fp, NEC_ES); + MacroLoadNecRegisterFromFile(fp, NEC_SS); + MacroLoadNecRegisterFromFile(fp, NEC_IX); + MacroLoadNecRegisterFromFile(fp, NEC_IY); + MacroLoadNecRegisterFromFile(fp, NEC_BP); + MacroLoadNecRegisterFromFile(fp, NEC_SP); + MacroLoadNecRegisterFromFile(fp, NEC_FLAGS); + MacroLoadNecRegisterFromFile(fp, NEC_VECTOR); + MacroLoadNecRegisterFromFile(fp, NEC_PENDING); + MacroLoadNecRegisterFromFile(fp, NEC_NMI_STATE); + MacroLoadNecRegisterFromFile(fp, NEC_IRQ_STATE); - read(fp,internalRam,65536); - read(fp,ws_staticRam,65536); - read(fp,ws_ioRam,256); - read(fp,ws_paletteColors,8); - read(fp,ws_palette,16*4*2); - read(fp,wsc_palette,16*16*2); - read(fp,&ws_newVideoMode,1); - read(fp,&ws_gpu_scanline,1); - read(fp,externalEeprom,131072); + read(fp, internalRam, 65536); + read(fp, ws_staticRam, 65536); + read(fp, ws_ioRam, 256); + read(fp, ws_paletteColors, 8); + read(fp, ws_palette, 16 * 4 * 2); + read(fp, wsc_palette, 16 * 16 * 2); + read(fp, &ws_newVideoMode, 1); + read(fp, &ws_gpu_scanline, 1); + read(fp, externalEeprom, 131072); - ws_audio_readState(fp); - close(fp); + ws_audio_readState(fp); + close(fp); - // force a video mode change to make all tiles dirty - ws_gpu_clearCache(); - return(1); + // force a video mode change to make all tiles dirty + ws_gpu_clearCache(); + return (1); } //////////////////////////////////////////////////////////////////////////////// // @@ -391,89 +399,90 @@ int ws_loadState(char *statepath) // // //////////////////////////////////////////////////////////////////////////////// -#define MacroStoreNecRegisterToFile(F,R) \ - value=nec_get_reg(R); \ - write(fp,&value,sizeof(value)); +#define MacroStoreNecRegisterToFile(F, R) \ + value=nec_get_reg(R); \ + write(fp,&value,sizeof(value)); -int ws_saveState(char *statepath) +int ws_saveState(char *statepath) { - uint16_t crc=memory_getRomCrc(); - uint32_t value; - char newPath[1024]; - fprintf(log_get(),"saving %s\n",statepath); + uint16_t crc = memory_getRomCrc(); + uint32_t value; + char newPath[1024]; + fprintf(log_get(), "saving %s\n", statepath); - if (strlen(statepath)<4) - { - sprintf(newPath,"%s.wss",statepath); - } - else - { - int len=strlen(statepath); + if (strlen(statepath) < 4) + { + sprintf(newPath, "%s.wss", statepath); + } + else + { + int len = strlen(statepath); - if ((statepath[len-1]!='s')&&(statepath[len-1]!='S')) - { - sprintf(newPath,"%s.wss",statepath); - } - else if ((statepath[len-2]!='s')&&(statepath[len-2]!='S')) - { - sprintf(newPath,"%s.wss",statepath); - } - else if ((statepath[len-3]!='w')&&(statepath[len-3]!='w')) - { - sprintf(newPath,"%s.wss",statepath); - } - else if (statepath[len-4]!='.') - { - sprintf(newPath,"%s.wss",statepath); - } - else - { - sprintf(newPath,"%s",statepath); - } - } + if ((statepath[len - 1] != 's') && (statepath[len - 1] != 'S')) + { + sprintf(newPath, "%s.wss", statepath); + } + else if ((statepath[len - 2] != 's') && (statepath[len - 2] != 'S')) + { + sprintf(newPath, "%s.wss", statepath); + } + else if ((statepath[len - 3] != 'w') && (statepath[len - 3] != 'w')) + { + sprintf(newPath, "%s.wss", statepath); + } + else if (statepath[len - 4] != '.') + { + sprintf(newPath, "%s.wss", statepath); + } + else + { + sprintf(newPath, "%s", statepath); + } + } - int fp=open(newPath, O_RDWR|O_CREAT, 0644); + int fp = open(newPath, O_RDWR | O_CREAT, 0644); - if (fp==-1) - { - return(0); - } + if (fp == -1) + { + return (0); + } - write(fp,&crc,2); - MacroStoreNecRegisterToFile(fp,NEC_IP); - MacroStoreNecRegisterToFile(fp,NEC_AW); - MacroStoreNecRegisterToFile(fp,NEC_BW); - MacroStoreNecRegisterToFile(fp,NEC_CW); - MacroStoreNecRegisterToFile(fp,NEC_DW); - MacroStoreNecRegisterToFile(fp,NEC_CS); - MacroStoreNecRegisterToFile(fp,NEC_DS); - MacroStoreNecRegisterToFile(fp,NEC_ES); - MacroStoreNecRegisterToFile(fp,NEC_SS); - MacroStoreNecRegisterToFile(fp,NEC_IX); - MacroStoreNecRegisterToFile(fp,NEC_IY); - MacroStoreNecRegisterToFile(fp,NEC_BP); - MacroStoreNecRegisterToFile(fp,NEC_SP); - MacroStoreNecRegisterToFile(fp,NEC_FLAGS); - MacroStoreNecRegisterToFile(fp,NEC_VECTOR); - MacroStoreNecRegisterToFile(fp,NEC_PENDING); - MacroStoreNecRegisterToFile(fp,NEC_NMI_STATE); - MacroStoreNecRegisterToFile(fp,NEC_IRQ_STATE); + write(fp, &crc, 2); + MacroStoreNecRegisterToFile(fp, NEC_IP); + MacroStoreNecRegisterToFile(fp, NEC_AW); + MacroStoreNecRegisterToFile(fp, NEC_BW); + MacroStoreNecRegisterToFile(fp, NEC_CW); + MacroStoreNecRegisterToFile(fp, NEC_DW); + MacroStoreNecRegisterToFile(fp, NEC_CS); + MacroStoreNecRegisterToFile(fp, NEC_DS); + MacroStoreNecRegisterToFile(fp, NEC_ES); + MacroStoreNecRegisterToFile(fp, NEC_SS); + MacroStoreNecRegisterToFile(fp, NEC_IX); + MacroStoreNecRegisterToFile(fp, NEC_IY); + MacroStoreNecRegisterToFile(fp, NEC_BP); + MacroStoreNecRegisterToFile(fp, NEC_SP); + MacroStoreNecRegisterToFile(fp, NEC_FLAGS); + MacroStoreNecRegisterToFile(fp, NEC_VECTOR); + MacroStoreNecRegisterToFile(fp, NEC_PENDING); + MacroStoreNecRegisterToFile(fp, NEC_NMI_STATE); + MacroStoreNecRegisterToFile(fp, NEC_IRQ_STATE); - write(fp,internalRam,65536); - write(fp,ws_staticRam,65536); - write(fp,ws_ioRam,256); - write(fp,ws_paletteColors,8); - write(fp,ws_palette,16*4*2); - write(fp,wsc_palette,16*16*2); - write(fp,&ws_videoMode,1); - write(fp,&ws_gpu_scanline,1); - write(fp,externalEeprom,131072); + write(fp, internalRam, 65536); + write(fp, ws_staticRam, 65536); + write(fp, ws_ioRam, 256); + write(fp, ws_paletteColors, 8); + write(fp, ws_palette, 16 * 4 * 2); + write(fp, wsc_palette, 16 * 16 * 2); + write(fp, &ws_videoMode, 1); + write(fp, &ws_gpu_scanline, 1); + write(fp, externalEeprom, 131072); - ws_audio_writeState(fp); - close(fp); + ws_audio_writeState(fp); + close(fp); - return(1); + return (1); } + //////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// @@ -487,8 +496,8 @@ int ws_saveState(char *statepath) //////////////////////////////////////////////////////////////////////////////// int ws_rotated(void) { - uint8_t *rom=memory_getRom(); - uint32_t romSize=memory_getRomSize(); + uint8_t *rom = memory_getRom(); + uint32_t romSize = memory_getRomSize(); - return(rom[romSize-4]&1); + return (rom[romSize - 4] & 1); } diff --git a/source/ws.h b/source/ws.h index eff8b76..e6b5f64 100644 --- a/source/ws.h +++ b/source/ws.h @@ -1,5 +1,10 @@ -////////////////////////////////////////////////////////////////////////////// -// +/* + * NewOswan + * ws.h: + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ ////////////////////////////////////////////////////////////////////////////// // // @@ -14,25 +19,23 @@ #include -typedef enum wssystem_t { - WS_SYSTEM_AUTODETECT = 0, - WS_SYSTEM_MONO, - WS_SYSTEM_COLOR, - WS_SYSTEM_CRYSTAL, +typedef enum wssystem_t +{ + WS_SYSTEM_AUTODETECT = 0, WS_SYSTEM_MONO, WS_SYSTEM_COLOR, WS_SYSTEM_CRYSTAL, } wssystem_t; -extern uint32_t ws_cyclesByLine; +extern uint32_t ws_cyclesByLine; -int ws_init(char *rompath); -int ws_rotated(void); -void ws_set_system(wssystem_t system); -wssystem_t ws_get_system(); -void ws_reset(void); -int ws_executeLine(int16_t *framebuffer, int renderLine); -void ws_patchRom(void); -int ws_loadState(char *statepath); -int ws_saveState(char *statepath); -void ws_done(void); +int ws_init(char *rompath); +int ws_rotated(void); +void ws_set_system(wssystem_t system); +wssystem_t ws_get_system(); +void ws_reset(void); +int ws_executeLine(int16_t *framebuffer, int renderLine); +void ws_patchRom(void); +int ws_loadState(char *statepath); +int ws_saveState(char *statepath); +void ws_done(void); extern char *ws_sram_path; extern char *ws_ieep_path; diff --git a/testserial.c b/testserial.c index 9534360..9317267 100644 --- a/testserial.c +++ b/testserial.c @@ -1,3 +1,10 @@ +/* + * NewOswan + * testserial.c: A simple tool to test serial in/out + * Based on the original Oswan-unix + * Copyright (c) 2014-2021 986-Studio. All rights reserved. + * + */ #include #include #include