Commit 33fb0536 authored by Sergio Costas's avatar Sergio Costas

Passed all the code through uncrustify

parent 85e33c3d
......@@ -28,14 +28,13 @@
#include "spk_ay.hh"
void uncompress_z80(FILE *fichero, int length, unsigned char *memo) {
unsigned char byte_loaded, EDfound, counter;
int position;
int position;
counter = 0;
counter = 0;
byte_loaded = 0;
EDfound = 0;
position = 0;
EDfound = 0;
position = 0;
printf("Descomprimo de longitud %d\n", length);
......@@ -44,8 +43,9 @@ void uncompress_z80(FILE *fichero, int length, unsigned char *memo) {
memo[position++] = byte_loaded;
counter--;
continue;
} else
} else {
fread(&byte_loaded, 1, 1, fichero);
}
if (EDfound == 2) { // we have two EDs
counter = byte_loaded;
......@@ -59,7 +59,7 @@ void uncompress_z80(FILE *fichero, int length, unsigned char *memo) {
} else {
if (EDfound == 1) { // we found ED xx. We write ED and xx
memo[position++] = 0xED;
EDfound = 0;
EDfound = 0;
}
if (position >= length) {
break;
......@@ -67,12 +67,10 @@ void uncompress_z80(FILE *fichero, int length, unsigned char *memo) {
memo[position++] = byte_loaded;
}
} while (position < length);
}
int save_z80(char *filename) {
FILE *fichero;
FILE * fichero;
unsigned char value, bucle;
fichero = fopen(filename, "r");
......@@ -83,39 +81,41 @@ int save_z80(char *filename) {
fichero = fopen(filename, "w");
if (fichero == NULL)
if (fichero == NULL) {
return -2; // can't create file
}
fprintf(fichero, "%c%c%c%c%c%c", procesador.Rm.br.A, procesador.Rm.br.F,
procesador.Rm.br.C, procesador.Rm.br.B, procesador.Rm.br.L,
procesador.Rm.br.H); // AF, BC and HL
procesador.Rm.br.C, procesador.Rm.br.B, procesador.Rm.br.L,
procesador.Rm.br.H); // AF, BC and HL
if (ordenador->current_mode == MODE_48K) {// 48K
if (ordenador->current_mode == MODE_48K) { // 48K
fprintf(fichero, "%c%c", (byte) (procesador.PC & 0x0FF), (byte) ((procesador.PC >> 8) & 0xFF)); // PC
} else {
fprintf(fichero, "%c%c", 0, 0); // 128K
fprintf(fichero, "%c%c", 0, 0); // 128K
}
fprintf(fichero, "%c%c", procesador.Rm.br.P, procesador.Rm.br.S); // SP
fprintf(fichero, "%c%c", procesador.Rm.br.P, procesador.Rm.br.S); // SP
fprintf(fichero, "%c%c%c", procesador.I, procesador.R,
(((procesador.R2 >> 7) & 0x01) | ((screen->border << 1) & 0x0E))); // I, R and border color
(((procesador.R2 >> 7) & 0x01) | ((screen->border << 1) & 0x0E))); // I, R and border color
fprintf(fichero, "%c%c%c%c%c%c%c%c%c%c%c%c%c%c", procesador.Rm.br.E,
procesador.Rm.br.D, procesador.Ra.br.C, procesador.Ra.br.B,
procesador.Ra.br.E, procesador.Ra.br.D, procesador.Ra.br.L,
procesador.Ra.br.H, procesador.Ra.br.A, procesador.Ra.br.F,
procesador.Rm.br.IYl, procesador.Rm.br.IYh, procesador.Rm.br.IXl,
procesador.Rm.br.IXh);
procesador.Rm.br.D, procesador.Ra.br.C, procesador.Ra.br.B,
procesador.Ra.br.E, procesador.Ra.br.D, procesador.Ra.br.L,
procesador.Ra.br.H, procesador.Ra.br.A, procesador.Ra.br.F,
procesador.Rm.br.IYl, procesador.Rm.br.IYh, procesador.Rm.br.IXl,
procesador.Rm.br.IXh);
if (procesador.IFF1)
if (procesador.IFF1) {
value = 1;
else
} else {
value = 0;
}
fprintf(fichero, "%c", value);
if (procesador.IFF2)
if (procesador.IFF2) {
value = 1;
else
} else {
value = 0;
}
fprintf(fichero, "%c", value);
value = procesador.IM;
if (!ordenador->issue_3) {
......@@ -125,36 +125,39 @@ int save_z80(char *filename) {
case JOYSTICK_KEMPSTON:
value |= 64;
break;
case JOYSTICK_SINCLAIR1:
value |= 128;
break;
case JOYSTICK_SINCLAIR2:
value |= 192;
break;
}
fprintf(fichero, "%c", value);
if (ordenador->current_mode == MODE_48K) { // 48K
if (ordenador->current_mode == MODE_48K) { // 48K
fwrite((ordenador->memoria + 147456), 16384, 1, fichero); // video memory
fwrite((ordenador->memoria + 98304), 32768, 1, fichero); // memory pages 2 & 3
fwrite((ordenador->memoria + 98304), 32768, 1, fichero); // memory pages 2 & 3
fclose(fichero);
return 0;
}
// 128K
fprintf(fichero, "%c%c", 23, 0); // Z80 file v2.01
fprintf(fichero, "%c%c", 23, 0); // Z80 file v2.01
fprintf(fichero, "%c%c", (byte) (procesador.PC & 0x0FF),
(byte) ((procesador.PC >> 8) & 0x0FF)); // PC
fprintf(fichero, "%c", 3); // hardware mode=3
fprintf(fichero, "%c", ordenador->mport1); // content of 0x7FFD latch
fprintf(fichero, "%c%c", 0, 0); // no If1, no emulation of any kind
fprintf(fichero, "%c", spk_ay->get_latch()); // last selected AY register
for (bucle = 0; bucle < 16; bucle++)
(byte) ((procesador.PC >> 8) & 0x0FF)); // PC
fprintf(fichero, "%c", 3); // hardware mode=3
fprintf(fichero, "%c", ordenador->mport1); // content of 0x7FFD latch
fprintf(fichero, "%c%c", 0, 0); // no If1, no emulation of any kind
fprintf(fichero, "%c", spk_ay->get_latch()); // last selected AY register
for (bucle = 0; bucle < 16; bucle++) {
fprintf(fichero, "%c", spk_ay->get_value(bucle)); // AY registers
}
for (bucle = 0; bucle < 8; bucle++) {
fprintf(fichero, "%c%c", 0xFF, 0xFF); // length=0xFFFF (uncompressed)
fprintf(fichero, "%c", bucle + 3); // page number
fprintf(fichero, "%c%c", 0xFF, 0xFF); // length=0xFFFF (uncompressed)
fprintf(fichero, "%c", bucle + 3); // page number
fwrite(ordenador->memoria + (16384 * bucle) + 65536, 16384, 1, fichero); // store page
}
fclose(fichero);
......@@ -162,19 +165,17 @@ int save_z80(char *filename) {
}
int load_z80(const char *filename) {
struct z80snapshot *snap;
unsigned char tempo[30], tempo2[56], type, compressed, page, byte_read[3];
unsigned char *memo;
unsigned char tempo[30], tempo2[56], type, compressed, page, byte_read[3];
unsigned char * memo;
FILE *fichero;
int longitud = 0, longitud2, bucle;
int longitud = 0, longitud2, bucle;
memo = (unsigned char *) malloc(49152);
snap = (struct z80snapshot *) malloc(sizeof(struct z80snapshot));
longitud = strlen(filename);
if ((longitud > 4)
&& (0 == strcasecmp(".sna", filename + (longitud - 4)))) {
if ((longitud > 4) && (0 == strcasecmp(".sna", filename + (longitud - 4)))) {
printf("Read SNA file\n");
free(memo);
free(snap);
......@@ -183,8 +184,9 @@ int load_z80(const char *filename) {
printf("Read Z80 file\n");
for (bucle = 0; bucle < 16; bucle++)
for (bucle = 0; bucle < 16; bucle++) {
snap->ay_regs[bucle] = 0;
}
fichero = fopen(filename, "r");
if (fichero == NULL) {
......@@ -198,7 +200,7 @@ int load_z80(const char *filename) {
if ((tempo[6] == 0) && (tempo[7] == 0)) { // extended Z80
printf("It's an extended Z80 file\n");
type = 1; // new type
type = 1; // new type
fread(tempo2, 1, 2, fichero); // read the length of the extension
......@@ -213,25 +215,28 @@ int load_z80(const char *filename) {
printf("Length: %d\n", longitud);
fread(tempo2 + 2, 1, longitud, fichero);
if (longitud == 23) // z80 ver 2.01
if (longitud == 23) { // z80 ver 2.01
switch (tempo2[4]) {
case 0:
case 1:
snap->type = 0; // 48K
break;
case 3:
case 4:
snap->type = 1; // 128K
break;
default:
fclose(fichero);
printf("Again not suported Z80 file\n");
free(memo);
free(snap);
return -3; // not a supported Z80 file
break;
}
else
} else {
// z80 ver 3.0x
switch (tempo2[4]) {
case 0:
......@@ -239,27 +244,31 @@ int load_z80(const char *filename) {
case 3:
snap->type = 0; // 48K
break;
case 4:
case 5:
case 6:
snap->type = 1; // 128K
break;
default:
fclose(fichero);
free(memo);
free(snap);
return -3; // not a supported Z80 file
break;
}
}
} else {
printf("Old type z80\n");
type = 0; // old type
type = 0; // old type
snap->type = 0; // 48k
}
if (tempo[29] & 0x04) {
printf("Issue 2\n");
snap->issue_3= false; // issue2
snap->issue_3 = false; // issue2
} else {
printf("Issue 3\n");
snap->issue_3 = true; // issue3
......@@ -273,16 +282,17 @@ int load_z80(const char *filename) {
snap->H = tempo[5];
if (type) {
snap->PC = ((word) tempo2[2]) + 256 * ((word) tempo2[3]);
for (bucle = 0; bucle < 16; bucle++)
for (bucle = 0; bucle < 16; bucle++) {
snap->ay_regs[bucle] = tempo2[9 + bucle];
}
snap->ay_latch = tempo2[8];
} else {
snap->PC = ((word) tempo[6]) + 256 * ((word) tempo[7]);
}
snap->SP = ((word) tempo[8]) + 256 * ((word) tempo[9]);
snap->I = tempo[10];
snap->R = (tempo[11] & 0x7F);
snap->I = tempo[10];
snap->R = (tempo[11] & 0x7F);
if (tempo[12] == 255) {
printf("Byte 12 is 255! doing 1\n");
......@@ -301,8 +311,8 @@ int load_z80(const char *filename) {
compressed = 0;
}
snap->E = tempo[13];
snap->D = tempo[14];
snap->E = tempo[13];
snap->D = tempo[14];
snap->CC = tempo[15];
snap->BB = tempo[16];
snap->EE = tempo[17];
......@@ -314,23 +324,27 @@ int load_z80(const char *filename) {
snap->IY = ((word) tempo[23]) + 256 * ((word) tempo[24]);
snap->IX = ((word) tempo[25]) + 256 * ((word) tempo[26]);
if (tempo[27] != 0)
if (tempo[27] != 0) {
snap->IFF1 = 1;
else
} else {
snap->IFF1 = 0;
}
if (tempo[28] != 0)
if (tempo[28] != 0) {
snap->IFF2 = 1;
else
} else {
snap->IFF2 = 0;
}
switch (tempo[29] & 0x03) {
case 0:
snap->Imode = 0;
break;
case 1:
snap->Imode = 1;
break;
case 2:
snap->Imode = 2;
break;
......@@ -338,83 +352,97 @@ int load_z80(const char *filename) {
snap->joystick = ((tempo[29] >> 6) & 0x03);
if (type)
if (type) {
snap->pager = tempo2[5];
}
if (type) { // extended z80
if (snap->type == 1) { // 128K snapshot
/* fclose(fichero);
return -3;*/ // z80 file not yet supported
* return -3;*/
// z80 file not yet supported
while (!feof(fichero)) {
fread(byte_read, 3, 1, fichero);
if (feof(fichero))
if (feof(fichero)) {
break;
}
longitud2 = ((int) byte_read[0]) + 256 * ((int) byte_read[1]);
switch (byte_read[2]) {
case 3:
page = 0;
break;
case 4:
page = 1;
break;
case 5:
page = 2;
break;
case 6:
page = 3;
break;
case 7:
page = 4;
break;
case 8:
page = 5;
break;
case 9:
page = 6;
break;
case 10:
page = 7;
break;
default:
page = 11;
break;
}
printf("Loading page %d of length %d\n", page, longitud);
if (longitud2 == 0xFFFF) // uncompressed raw data
if (longitud2 == 0xFFFF) { // uncompressed raw data
fread(snap->page[page], 16384, 1, fichero);
else
} else {
uncompress_z80(fichero, 16384, snap->page[page]);
}
}
} else {
while (!feof(fichero)) {
fread(byte_read, 3, 1, fichero);
if (feof(fichero))
if (feof(fichero)) {
break;
}
longitud2 = ((int) byte_read[0]) + 256 * ((int) byte_read[1]);
switch (byte_read[2]) {
case 8:
page = 0;
break;
case 4:
page = 1;
break;
case 5:
page = 2;
break;
default:
page = 11;
break;
}
if (longitud2 == 0xFFFF) // uncompressed raw data
if (longitud2 == 0xFFFF) { // uncompressed raw data
fread(snap->page[page], 16384, 1, fichero);
else
} else {
uncompress_z80(fichero, 16384, snap->page[page]);
}
}
}
} else {
if (compressed) {
// 48k compressed z80 loader
......@@ -425,7 +453,6 @@ int load_z80(const char *filename) {
memcpy(snap->page[0], memo, 16384);
memcpy(snap->page[1], memo + 16384, 16384);
memcpy(snap->page[2], memo + 32768, 16384);
} else {
// 48k uncompressed z80 loader
......@@ -433,7 +460,6 @@ int load_z80(const char *filename) {
fread(snap->page[1], 16384, 1, fichero);
fread(snap->page[2], 16384, 1, fichero);
}
}
load_snap(snap);
......@@ -444,18 +470,17 @@ int load_z80(const char *filename) {
}
int load_sna(const char *filename) {
unsigned char *tempo;
unsigned char *tempo2;
unsigned char type = 0;
FILE *fichero;
unsigned char * tempo;
unsigned char * tempo2;
unsigned char type = 0;
FILE * fichero;
struct z80snapshot *snap;
unsigned char v1, v2;
unsigned char v1, v2;
int addr, loop;
tempo = (unsigned char *) malloc(49179);
tempo = (unsigned char *) malloc(49179);
tempo2 = (unsigned char *) malloc(98308);
snap = (struct z80snapshot *) malloc(sizeof(struct z80snapshot));
snap = (struct z80snapshot *) malloc(sizeof(struct z80snapshot));
printf("Loading SNA file\n");
......@@ -485,14 +510,15 @@ int load_sna(const char *filename) {
snap->type = type;
//Some inits
for (loop = 0; loop < 16; loop++)
// Some inits
for (loop = 0; loop < 16; loop++) {
snap->ay_regs[loop] = 0;
}
snap->ay_latch = 0;
snap->issue_3 = true;
snap->joystick = 1; //kempston
snap->issue_3 = true;
snap->joystick = 1; // kempston
snap->I = tempo[0];
snap->I = tempo[0];
snap->LL = tempo[1];
snap->HH = tempo[2];
snap->EE = tempo[3];
......@@ -524,17 +550,16 @@ int load_sna(const char *filename) {
snap->IFF2 = 0;
}
snap->R = tempo[20];
snap->F = tempo[21];
snap->A = tempo[22];
snap->SP = ((word) tempo[23]) + 256 * ((word) tempo[24]);
snap->Imode = tempo[25];
snap->R = tempo[20];
snap->F = tempo[21];
snap->A = tempo[22];
snap->SP = ((word) tempo[23]) + 256 * ((word) tempo[24]);
snap->Imode = tempo[25];
snap->border = tempo[26];
if (type == 0) {
v1 = tempo[23];
v2 = tempo[24];
v1 = tempo[23];
v2 = tempo[24];
addr = ((int) v1) + 256 * ((int) v2);
if ((addr < 16384) || (addr >= 65534)) {
free(tempo);
......@@ -544,13 +569,13 @@ int load_sna(const char *filename) {
printf("Error loading SNA file. Return address in ROM.\n");
return -1;
}
addr -= 16384;
addr += 27;
snap->PC = ((word) tempo[addr]) + 256 * ((word) tempo[addr + 1]);
tempo[addr] = 0;
addr -= 16384;
addr += 27;
snap->PC = ((word) tempo[addr]) + 256 * ((word) tempo[addr + 1]);
tempo[addr] = 0;
tempo[addr + 1] = 0;
snap->SP += 2;
snap->IFF1 = snap->IFF2;
snap->SP += 2;
snap->IFF1 = snap->IFF2;
memcpy(snap->page[0], tempo + 27, 16384);
memcpy(snap->page[1], tempo + 16411, 16384);
memcpy(snap->page[2], tempo + 32795, 16384);
......@@ -558,9 +583,9 @@ int load_sna(const char *filename) {
snap->PC = ((word) tempo2[0]) + 256 * ((word) tempo2[1]);
memcpy(snap->page[5], tempo + 27, 16384);
memcpy(snap->page[2], tempo + 16411, 16384);
v1 = tempo[2];
v1 = tempo[2];
snap->pager = v1;
v1 &= 0x07;
v1 &= 0x07;
memcpy(snap->page[v1], tempo + 32795, 16384);
addr = 4;
for (loop = 0; loop < 7; loop++) {
......@@ -578,30 +603,30 @@ int load_sna(const char *filename) {
free(snap);
fclose(fichero);
return 0;
}
void load_snap(struct z80snapshot *snap) {
int bucle;
printf("Loading SnapShot\n");
switch (snap->type) {
case 0: // 48k
case 0: // 48k
printf("Mode 48K\n");
ordenador->current_mode = MODE_48K; // 48K mode
ordenador->issue_3 = snap->issue_3;
ordenador->issue_3 = snap->issue_3;
ResetComputer();
break;
case 1: // 128k
case 1: // 128k
printf("Mode 128K\n");
ordenador->current_mode = MODE_P2; // +2 mode
ordenador->issue_3 = true;
ordenador->issue_3 = true;
ResetComputer();
printf("Pager: %X\n", snap->pager);
Z80free_Out(0x7FFD, snap->pager);
break;
default:
break;
}
......@@ -609,16 +634,19 @@ void load_snap(struct z80snapshot *snap) {
switch (snap->joystick) {
case 0:
keyboard->joystick = JOYSTICK_CURSOR;
break;
break;
case 1:
keyboard->joystick = JOYSTICK_KEMPSTON;
break;
break;
case 2:
keyboard->joystick = JOYSTICK_SINCLAIR1;
break;
break;
case 3:
keyboard->joystick = JOYSTICK_SINCLAIR2;
break;
break;
}
procesador.Rm.br.A = snap->A;
......@@ -630,7 +658,7 @@ void load_snap(struct z80snapshot *snap) {
procesador.Rm.br.H = snap->H;
procesador.Rm.br.L = snap->L;
printf("A:%d F:%d B:%d C:%d D:%d E:%d H:%d L:%d\n", snap->A, snap->F,
snap->B, snap->C, snap->D, snap->E, snap->H, snap->L);
snap->B, snap->C, snap->D, snap->E, snap->H, snap->L);
procesador.Ra.br.A = snap->AA;
procesador.Ra.br.F = snap->FF;
procesador.Ra.br.B = snap->BB;
......@@ -640,14 +668,14 @@ void load_snap(struct z80snapshot *snap) {
procesador.Ra.br.H = snap->HH;
procesador.Ra.br.L = snap->LL;
printf("A:%d F:%d B:%d C:%d D:%d E:%d H:%d L:%d\n", snap->AA, snap->FF,
snap->BB, snap->CC, snap->DD, snap->EE, snap->HH, snap->LL);
snap->BB, snap->CC, snap->DD, snap->EE, snap->HH, snap->LL);
procesador.Rm.wr.IX = snap->IX;
procesador.Rm.wr.IY = snap->IY;
procesador.Rm.wr.SP = snap->SP;
procesador.PC = snap->PC;
procesador.PC = snap->PC;
printf("IX:%d IY:%d SP:%d PC:%d\n", snap->IX, snap->IY, snap->SP, snap->PC);
procesador.I = snap->I;
procesador.R = snap->R;
procesador.I = snap->I;
procesador.R = snap->R;
procesador.R2 = snap->R;
printf("I:%d R:%d\n", snap->I, snap->R);
......@@ -670,18 +698,19 @@ void load_snap(struct z80snapshot *snap) {
for (bucle = 0; bucle < 16384; bucle++) {
ordenador->memoria[bucle + 147456] = snap->page[0][bucle];
ordenador->memoria[bucle + 98304] = snap->page[1][bucle];
ordenador->memoria[bucle + 98304] = snap->page[1][bucle];
ordenador->memoria[bucle + 114688] = snap->page[2][bucle];
}
spk_ay->ay_emul = 0;
break;
case 1: // 128K
for (bucle = 0; bucle < 16384; bucle++) {
ordenador->memoria[bucle + 65536] = snap->page[0][bucle];
ordenador->memoria[bucle + 81920] = snap->page[1][bucle];
ordenador->memoria[bucle + 98304] = snap->page[2][bucle];
ordenador->memoria[bucle + 65536] = snap->page[0][bucle];
ordenador->memoria[bucle + 81920] = snap->page[1][bucle];
ordenador->memoria[bucle + 98304] = snap->page[2][bucle];
ordenador->memoria[bucle + 114688] = snap->page[3][bucle];
ordenador->memoria[bucle + 131072] = snap->page[4][bucle];
ordenador->memoria[bucle + 147456] = snap->page[5][bucle];
......@@ -695,6 +724,7 @@ void load_snap(struct z80snapshot *snap) {
}
spk_ay->set_latch(snap->ay_latch);
break;
default:
break;
}
......
......@@ -22,23 +22,21 @@
#include "z80free/Z80free.h"
struct z80snapshot {
byte A,F,B,C,D,E,H,L,AA,FF,BB,CC,DD,EE,HH,LL,R,I,IFF1,IFF2,Imode;
bool issue_3;
word PC,IX,IY,SP;
byte type; // bit 0/1: 48K/128K/+3
byte border; // border color
byte pager; // content of pagination register in 128K mode
unsigned char page[12][16384];
unsigned int found_pages; // bit=1: page exists. bit=0: page don't exists.
unsigned char ay_regs[16];
unsigned char ay_latch;
unsigned char joystick;
byte A, F, B, C, D, E, H, L, AA, FF, BB, CC, DD, EE, HH, LL, R, I, IFF1, IFF2, Imode;
bool issue_3;
word PC, IX, IY, SP;
byte type; // bit 0/1: 48K/128K/+3
byte border; // border color
byte pager; // content of pagination register in 128K mode
unsigned char page[12][16384];
unsigned int found_pages; // bit=1: page exists. bit=0: page don't exists.
unsigned char ay_regs[16];
unsigned char ay_latch;
unsigned char joystick;
};
int save_z80(char *);
int load_z80(const char *);
int load_sna(const char *);
void load_snap(struct z80snapshot *);
void uncompress_z80(FILE *,int,unsigned char *);
void uncompress_z80(FILE *, int, unsigned char *);
......@@ -12,31 +12,30 @@ using namespace std;
#include <stdlib.h>
#include "cmdline.hh"
CMDLine::CMDLine(int argc,char **argv) {
CMDLine::CMDLine(int argc, char **argv) {
int argumento;