]> git.sesse.net Git - pistorm/blob - buptest.c
Fix up buptest after IDE emulation layer removal
[pistorm] / buptest.c
1 // SPDX-License-Identifier: MIT
2
3 #include <assert.h>
4 #include <dirent.h>
5 #include <endian.h>
6 #include <fcntl.h>
7 #include <pthread.h>
8 #include <sched.h>
9 #include <signal.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <time.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/mman.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19 #include <sys/ioctl.h>
20 #include "emulator.h"
21 #include "gpio/ps_protocol.h"
22
23 #define SIZE_KILO 1024
24 #define SIZE_MEGA (1024 * 1024)
25 #define SIZE_GIGA (1024 * 1024 * 1024)
26
27 uint8_t garbege_datas[2 * SIZE_MEGA];
28
29 struct timespec f2;
30
31 uint8_t gayle_int;
32 uint32_t mem_fd;
33 uint32_t errors = 0;
34 uint8_t loop_tests = 0, total_errors = 0;
35
36 void sigint_handler(int sig_num) {
37   printf("Received sigint %d, exiting.\n", sig_num);
38   printf("Total number of transaction errors occured: %d\n", total_errors);
39   if (mem_fd)
40     close(mem_fd);
41
42   exit(0);
43 }
44
45 int main(int argc, char *argv[]) {
46     uint32_t test_size = 512 * SIZE_KILO, cur_loop = 0;
47
48     clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &f2);
49     srand((unsigned int)f2.tv_nsec);
50
51     signal(SIGINT, sigint_handler);
52
53     ps_setup_protocol();
54     ps_reset_state_machine();
55     ps_pulse_reset();
56
57     usleep(1500);
58
59     write8(0xbfe201, 0x0101);       //CIA OVL
60         write8(0xbfe001, 0x0000);       //CIA OVL LOW
61
62     if (argc > 1) {
63         test_size = atoi(argv[1]) * SIZE_KILO;
64         if (test_size == 0 || test_size > 2 * SIZE_MEGA) {
65             test_size = 512 * SIZE_KILO;
66         }
67         printf("Testing %d KB of memory.\n", test_size / SIZE_KILO);
68         if (argc > 2) {
69             if (strcmp(argv[2], "l") == 0) {
70                 printf("Looping tests.\n");
71                 loop_tests = 1;
72             }
73         }
74     }
75
76 test_loop:;
77     printf("Writing garbege datas.\n");
78     for (uint32_t i = 0; i < test_size; i++) {
79         while(garbege_datas[i] == 0x00)
80             garbege_datas[i] = (uint8_t)(rand() % 0xFF);
81         write8(i, (uint32_t)garbege_datas[i]);
82     }
83
84     printf("Reading back garbege datas, read8()...\n");
85     for (uint32_t i = 0; i < test_size; i++) {
86         uint32_t c = read8(i);
87         if (c != garbege_datas[i]) {
88             if (errors < 512)
89                 printf("READ8: Garbege data mismatch at $%.6X: %.2X should be %.2X.\n", i, c, garbege_datas[i]);
90             errors++;
91         }
92     }
93     printf("read8 errors total: %d.\n", errors);
94     total_errors += errors;
95     errors = 0;
96     sleep (1);
97
98     printf("Reading back garbege datas, read16(), even addresses...\n");
99     for (uint32_t i = 0; i < (test_size) - 2; i += 2) {
100         uint32_t c = be16toh(read16(i));
101         if (c != *((uint16_t *)&garbege_datas[i])) {
102             if (errors < 512)
103                 printf("READ16_EVEN: Garbege data mismatch at $%.6X: %.4X should be %.4X.\n", i, c, *((uint16_t *)&garbege_datas[i]));
104             errors++;
105         }
106     }
107     printf("read16 even errors total: %d.\n", errors);
108     total_errors += errors;
109     errors = 0;
110     sleep (1);
111
112     printf("Reading back garbege datas, read16(), odd addresses...\n");
113     for (uint32_t i = 1; i < (test_size) - 2; i += 2) {
114         uint32_t c = be16toh((read8(i) << 8) | read8(i + 1));
115         if (c != *((uint16_t *)&garbege_datas[i])) {
116             if (errors < 512)
117                 printf("READ16_ODD: Garbege data mismatch at $%.6X: %.4X should be %.4X.\n", i, c, *((uint16_t *)&garbege_datas[i]));
118             errors++;
119         }
120     }
121     printf("read16 odd errors total: %d.\n", errors);
122     errors = 0;
123     sleep (1);
124
125     printf("Reading back garbege datas, read32(), even addresses...\n");
126     for (uint32_t i = 0; i < (test_size) - 4; i += 2) {
127         uint32_t c = be32toh(read32(i));
128         if (c != *((uint32_t *)&garbege_datas[i])) {
129             if (errors < 512)
130                 printf("READ32_EVEN: Garbege data mismatch at $%.6X: %.8X should be %.8X.\n", i, c, *((uint32_t *)&garbege_datas[i]));
131             errors++;
132         }
133     }
134     printf("read32 even errors total: %d.\n", errors);
135     total_errors += errors;
136     errors = 0;
137     sleep (1);
138
139     printf("Reading back garbege datas, read32(), odd addresses...\n");
140     for (uint32_t i = 1; i < (test_size) - 4; i += 2) {
141         uint32_t c = read8(i);
142         c |= (be16toh(read16(i + 1)) << 8);
143         c |= (read8(i + 3) << 24);
144         if (c != *((uint32_t *)&garbege_datas[i])) {
145             if (errors < 512)
146                 printf("READ32_ODD: Garbege data mismatch at $%.6X: %.8X should be %.8X.\n", i, c, *((uint32_t *)&garbege_datas[i]));
147             errors++;
148         }
149     }
150     printf("read32 odd errors total: %d.\n", errors);
151     total_errors += errors;
152     errors = 0;
153     sleep (1);
154
155     printf("Clearing %d KB of Chip again\n", test_size / SIZE_KILO);
156     for (uint32_t i = 0; i < test_size; i++) {
157         write8(i, (uint32_t)0x0);
158     }
159
160     printf("[WORD] Writing garbege datas to Chip, unaligned...\n");
161     for (uint32_t i = 1; i < (test_size) - 2; i += 2) {
162         uint16_t v = *((uint16_t *)&garbege_datas[i]);
163         write8(i, (v & 0x00FF));
164         write8(i + 1, (v >> 8));
165     }
166
167     sleep (1);
168     printf("Reading back garbege datas, read16(), odd addresses...\n");
169     for (uint32_t i = 1; i < (test_size) - 2; i += 2) {
170         uint32_t c = be16toh((read8(i) << 8) | read8(i + 1));
171         if (c != *((uint16_t *)&garbege_datas[i])) {
172             if (errors < 512)
173                 printf("READ16_ODD: Garbege data mismatch at $%.6X: %.4X should be %.4X.\n", i, c, *((uint16_t *)&garbege_datas[i]));
174             errors++;
175         }
176     }
177     printf("read16 odd errors total: %d.\n", errors);
178     total_errors += errors;
179     errors = 0;
180
181     printf("Clearing %d KB of Chip again\n", test_size / SIZE_KILO);
182     for (uint32_t i = 0; i < test_size; i++) {
183         write8(i, (uint32_t)0x0);
184     }
185
186     printf("[LONG] Writing garbege datas to Chip, unaligned...\n");
187     for (uint32_t i = 1; i < (test_size) - 4; i += 4) {
188         uint32_t v = *((uint32_t *)&garbege_datas[i]);
189         write8(i , v & 0x0000FF);
190         write16(i + 1, htobe16(((v & 0x00FFFF00) >> 8)));
191         write8(i + 3 , (v & 0xFF000000) >> 24);
192     }
193
194     sleep (1);
195     printf("Reading back garbege datas, read32(), odd addresses...\n");
196     for (uint32_t i = 1; i < (test_size) - 4; i += 4) {
197         uint32_t c = read8(i);
198         c |= (be16toh(read16(i + 1)) << 8);
199         c |= (read8(i + 3) << 24);
200         if (c != *((uint32_t *)&garbege_datas[i])) {
201             if (errors < 512)
202                 printf("READ32_ODD: Garbege data mismatch at $%.6X: %.8X should be %.8X.\n", i, c, *((uint32_t *)&garbege_datas[i]));
203             errors++;
204         }
205     }
206     printf("read32 odd errors total: %d.\n", errors);
207     total_errors += errors;
208     errors = 0;
209
210     if (loop_tests) {
211         printf ("Loop %d done. Begin loop %d.\n", cur_loop + 1, cur_loop + 2);
212         printf ("Current total errors: %d.\n", total_errors);
213         goto test_loop;
214     }
215
216     return 0;
217 }
218
219 void m68k_set_irq(unsigned int level) {
220 }