Skip to content

Commit 7e99afa

Browse files
committed
Improvements to step-test-Z80.
1 parent a81f4b1 commit 7e99afa

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

sources/step-test-Z80.c

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ static Z80 cpu;
9595
static Z80InsnClock insn_clock;
9696
static zuint8 memory[65536];
9797

98-
static Cycle *cycles;
98+
static Cycle *cycles = Z_NULL;
9999
static int cycles_size;
100100
static int cycles_index;
101101

102-
static Port *ports;
102+
static Port *ports = Z_NULL;
103103
static int ports_size;
104104
static int ports_index;
105105

@@ -109,27 +109,31 @@ static int expected_port_count;
109109
static zboolean file_failed, test_failed, array_failed;
110110
static char const *field_separator = "";
111111

112+
static zboolean cpu_break = Z_FALSE;
113+
112114

113115
static void add_cycle(zuint16 address, zsint16 value, char const *pins)
114116
{
115117
Cycle *c;
116118

117119
if (cycles_index == cycles_size)
118120
{
119-
if (cpu.cycles == cpu.cycle_limit) return;
121+
if (cpu_break) return;
120122
cycles_size += Z80_MAXIMUM_CYCLES_PER_STEP;
121123

122-
if ((cycles = realloc(cycles, cycles_size * sizeof(Cycle))) == Z_NULL)
124+
if ((c = realloc(cycles, cycles_size * sizeof(Cycle))) == Z_NULL)
123125
{
124126
z80_break(&cpu);
125127
return;
126128
}
129+
130+
cycles = c;
127131
}
128132

129-
c = cycles + cycles_index;
130-
c->address = address;
131-
c->value = value;
132-
memcpy(c->pins, pins, 4);
133+
c = cycles + cycles_index;
134+
c->address = address;
135+
c->value = value;
136+
*(zuint32 *)c->pins = *(zuint32 *)pins;
133137
cycles_index++;
134138
}
135139

@@ -144,17 +148,18 @@ static void add_port(zuint16 port, zuint8 value, char direction)
144148

145149
if (ports_index == ports_size)
146150
{
147-
if (cpu.cycles == cpu.cycle_limit) return;
151+
if (cpu_break) return;
148152
ports_size += Z80_MAXIMUM_CYCLES_PER_STEP;
149153

150-
if ((ports = realloc(ports, ports_size * sizeof(Port))) == Z_NULL)
154+
if ((p = realloc(ports, ports_size * sizeof(Port))) == Z_NULL)
151155
{
152156
z80_break(&cpu);
157+
cpu_break = Z_TRUE;
153158
return;
154159
}
155160
}
156161

157-
p = ports + ports_index;
162+
p = ports + ports_index;
158163
p->port = port;
159164
p->value = value;
160165
p->direction = direction;
@@ -310,7 +315,7 @@ static zboolean validate_test_state(cJSON *state)
310315

311316
cJSON_ArrayForEach(item, state)
312317
{
313-
const char *key = item->string;
318+
char const *key = item->string;
314319

315320
if (!strcmp(key, "ram"))
316321
{
@@ -807,25 +812,22 @@ int main(int argc, char **argv)
807812

808813
cJSON_ArrayForEach(test, tests)
809814
{
810-
cJSON* item;
811-
cJSON* initial = cJSON_GetObjectItem(test, "initial");
812-
cJSON* final = cJSON_GetObjectItem(test, "final" );
813-
cJSON* expected_cycles = cJSON_GetObjectItem(test, "cycles" );
814-
int expected_cycle_count = cJSON_GetArraySize (expected_cycles);
815-
cJSON *subitem_1, *subitem_2;
815+
cJSON *item, *subitem_1, *subitem_2;
816+
cJSON *initial = cJSON_GetObjectItem(test, "initial");
817+
cJSON *final = cJSON_GetObjectItem(test, "final" );
818+
cJSON *expected_cycles = cJSON_GetObjectItem(test, "cycles" );
819+
int expected_cycle_count = cJSON_GetArraySize (expected_cycles);
816820
zuint address, actual, expected;
817821

818-
/* Clean memory */
822+
/* Clean memory, power on CPU and start instruction clock. */
819823
memset(memory, 0, sizeof(memory));
820-
821-
/* Power on CPU */
822824
z80_power(&cpu, Z_TRUE);
823825
z80_insn_clock_start(&insn_clock, cpu.resume);
824826

825-
/* Set initial CPU state */
826-
for (j= 0; j != Z_ARRAY_SIZE(members) - 2; j++)
827+
/* Set initial CPU state. */
828+
for (j = 0; j != Z_ARRAY_SIZE(members) - 2; j++)
827829
{
828-
const Member *member = members + j;
830+
Member const *member = members + j;
829831
cJSON *value;
830832
double v = cJSON_GetNumberValue(cJSON_GetObjectItem(initial, member->key));
831833

@@ -844,7 +846,7 @@ int main(int argc, char **argv)
844846
cpu.data.uint8_array[1] = 0x57;
845847
}
846848

847-
/* Set initial memory state */
849+
/* Set initial memory state. */
848850
item = cJSON_GetObjectItem(initial, "ram");
849851

850852
cJSON_ArrayForEach(subitem_1, item) memory[
@@ -857,19 +859,18 @@ int main(int argc, char **argv)
857859
? cJSON_GetArraySize(expected_ports)
858860
: 0;
859861

860-
/* Run test */
862+
/* Run the test. */
863+
cpu_break = Z_FALSE;
861864
z80_run(&cpu, expected_cycle_count);
862-
863-
if ((cycles_size && cycles == Z_NULL) || (ports_size && ports == Z_NULL))
864-
goto cannot_allocate_memory;
865+
if (cpu_break) goto cannot_allocate_memory;
865866

866867
test_failed = array_failed = Z_FALSE;
867868
field_separator = "";
868869

869-
/* Check final CPU state */
870+
/* Check final CPU state. */
870871
for (j = 0; j != Z_ARRAY_SIZE(members) - 2; j++)
871872
{
872-
const Member *member = members + j;
873+
Member const *member = members + j;
873874

874875
expected = (zuint)cJSON_GetNumberValue(cJSON_GetObjectItem(final, member->key));
875876

@@ -913,7 +914,7 @@ int main(int argc, char **argv)
913914
field_separator = ",";
914915
}
915916

916-
/* Check final memory state */
917+
/* Check final memory state. */
917918
item = cJSON_GetObjectItem(final, "ram");
918919

919920
cJSON_ArrayForEach(subitem_1, item)
@@ -956,7 +957,7 @@ int main(int argc, char **argv)
956957

957958
array_check_end();
958959

959-
/* Check cycles */
960+
/* Check cycles. */
960961

961962
if (test_pins)
962963
{
@@ -986,7 +987,7 @@ int main(int argc, char **argv)
986987
field_separator = ",";
987988
}
988989

989-
/* Check ports */
990+
/* Check ports. */
990991

991992
for (j = 0; j != expected_port_count; j++)
992993
{
@@ -1039,7 +1040,7 @@ int main(int argc, char **argv)
10391040
return -1;
10401041
}
10411042

1042-
/* Print results summary */
1043+
/* Print results summary. */
10431044

10441045
printf( "\nResults:%c%d file%s in total",
10451046
test_format_and_exit ? ' ' : '\n',

0 commit comments

Comments
 (0)