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