forked from michalsc/Emu68
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestcode_m68k.c
607 lines (524 loc) · 16.7 KB
/
testcode_m68k.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#include <stdarg.h>
#include <stdint.h>
typedef unsigned int size_t;
void start();
void __boot()
{
start();
}
static inline __attribute__((always_inline)) uint64_t BE64(uint64_t x)
{
union {
uint64_t v;
uint8_t u[8];
} tmp;
tmp.v = x;
return ((uint64_t)(tmp.u[0]) << 56) | ((uint64_t)(tmp.u[1]) << 48) | ((uint64_t)(tmp.u[2]) << 40) | ((uint64_t)(tmp.u[3]) << 32) |
(tmp.u[4] << 24) | (tmp.u[5] << 16) | (tmp.u[6] << 8) | (tmp.u[7]);
}
static inline __attribute__((always_inline)) uint64_t LE64(uint64_t x)
{
union {
uint64_t v;
uint8_t u[8];
} tmp;
tmp.v = x;
return ((uint64_t)(tmp.u[7]) << 56) | ((uint64_t)(tmp.u[6]) << 48) | ((uint64_t)(tmp.u[5]) << 40) | ((uint64_t)(tmp.u[4]) << 32) |
(tmp.u[3] << 24) | (tmp.u[2] << 16) | (tmp.u[1] << 8) | (tmp.u[0]);
}
static inline __attribute__((always_inline)) uint32_t BE32(uint32_t x)
{
union {
uint32_t v;
uint8_t u[4];
} tmp;
tmp.v = x;
return (tmp.u[0] << 24) | (tmp.u[1] << 16) | (tmp.u[2] << 8) | (tmp.u[3]);
}
static inline __attribute__((always_inline)) uint32_t LE32(uint32_t x)
{
union {
uint32_t v;
uint8_t u[4];
} tmp;
tmp.v = x;
return (tmp.u[3] << 24) | (tmp.u[2] << 16) | (tmp.u[1] << 8) | (tmp.u[0]);
}
static inline __attribute__((always_inline)) uint16_t BE16(uint16_t x)
{
union {
uint16_t v;
uint8_t u[2];
} tmp;
tmp.v = x;
return (tmp.u[0] << 8) | (tmp.u[1]);
}
static inline __attribute__((always_inline)) uint16_t LE16(uint16_t x)
{
union {
uint16_t v;
uint8_t u[2];
} tmp;
tmp.v = x;
return (tmp.u[1] << 8) | (tmp.u[0]);
}
#define PL011_0_BASE (ARM_PERIIOBASE + 0x201000)
#define PRIMECELLID_PL011 0x011
#define PL011_DR (0x00)
#define PL011_RSRECR (0x04)
#define PL011_FR (0x18)
#define PL011_ILPR (0x20)
#define PL011_IBRD (0x24)
#define PL011_FBRD (0x28)
#define PL011_LCRH (0x2C)
#define PL011_CR (0x30)
#define PL011_IFLS (0x34)
#define PL011_IMSC (0x38)
#define PL011_RIS (0x3C)
#define PL011_MIS (0x40)
#define PL011_ICR (0x44)
#define PL011_DMACR (0x48)
#define PL011_ITCR (0x80)
#define PL011_ITIP (0x84)
#define PL011_ITOP (0x88)
#define PL011_TDR (0x8C)
#define PL011_FR_CTS (1 << 0)
#define PL011_FR_DSR (1 << 1)
#define PL011_FR_DCD (1 << 2)
#define PL011_FR_BUSY (1 << 3)
#define PL011_FR_RXFE (1 << 4)
#define PL011_FR_TXFF (1 << 5)
#define PL011_FR_RXFF (1 << 6)
#define PL011_FR_TXFE (1 << 7)
#define PL011_LCRH_BRK (1 << 0)
#define PL011_LCRH_PEN (1 << 1)
#define PL011_LCRH_EPS (1 << 2)
#define PL011_LCRH_STP2 (1 << 3)
#define PL011_LCRH_FEN (1 << 4)
#define PL011_LCRH_WLEN5 (0 << 5)
#define PL011_LCRH_WLEN6 (1 << 5)
#define PL011_LCRH_WLEN7 (2 << 5)
#define PL011_LCRH_WLEN8 (3 << 5)
#define PL011_LCRH_SPS (1 << 7)
#define PL011_CR_UARTEN (1 << 0)
#define PL011_CR_SIREN (1 << 1)
#define PL011_CR_SIRLP (1 << 2)
#define PL011_CR_LBE (1 << 7)
#define PL011_CR_TXE (1 << 8)
#define PL011_CR_RXE (1 << 9)
#define PL011_CR_RTSEN (1 << 14)
#define PL011_CR_CTSEN (1 << 15)
#define PL011_ICR_RIMIC (1 << 0)
#define PL011_ICR_CTSMIC (1 << 1)
#define PL011_ICR_DSRMIC (1 << 2)
#define PL011_ICR_DCDMIC (1 << 3)
#define PL011_ICR_RXIC (1 << 4)
#define PL011_ICR_TXIC (1 << 5)
#define PL011_ICR_RTIC (1 << 6)
#define PL011_ICR_FEIC (1 << 7)
#define PL011_ICR_PEIC (1 << 8)
#define PL011_ICR_BEIC (1 << 9)
#define PL011_ICR_OEIC (1 << 10)
static inline uint32_t rd32le(uint32_t iobase) {
return LE32(*(volatile uint32_t *)(iobase));
}
static inline uint32_t rd32be(uint32_t iobase) {
return BE32(*(volatile uint32_t *)(iobase));
}
static inline uint16_t rd16le(uint32_t iobase) {
return LE16(*(volatile uint16_t *)(iobase));
}
static inline uint16_t rd16be(uint32_t iobase) {
return BE16(*(volatile uint16_t *)(iobase));
}
static inline uint8_t rd8(uint32_t iobase) {
return *(volatile uint8_t *)(iobase);
}
static inline void wr32le(uint32_t iobase, uint32_t value) {
*(volatile uint32_t *)(iobase) = LE32(value);
}
static inline void wr32be(uint32_t iobase, uint32_t value) {
*(volatile uint32_t *)(iobase) = BE32(value);
}
static inline void wr16le(uint32_t iobase, uint16_t value) {
*(volatile uint16_t *)(iobase) = LE16(value);
}
static inline void wr16be(uint32_t iobase, uint16_t value) {
*(volatile uint16_t *)(iobase) = BE16(value);
}
static inline void wr8(uint32_t iobase, uint8_t value) {
*(volatile uint8_t *)(iobase) = value;
}
typedef void (*putc_func)(void *data, char c);
const char hello_msg[] = "[M68K] Hello from the m68k code\n";
int int_strlen(char *buf)
{
int len = 0;
if (buf)
while(*buf++)
len++;
return len;
}
void int_itoa(char *buf, char base, uintptr_t value, char zero_pad, int precision, int size_mod, char big, int alternate_form, int neg, char sign)
{
int length = 0;
do {
char c = value % base;
if (c >= 10) {
if (big)
c += 'A'-10;
else
c += 'a'-10;
}
else
c += '0';
value = value / base;
buf[length++] = c;
} while(value != 0);
if (precision != 0)
{
while (length < precision)
buf[length++] = '0';
}
else if (size_mod != 0 && zero_pad)
{
int sz_mod = size_mod;
if (alternate_form)
{
if (base == 16) sz_mod -= 2;
else if (base == 8) sz_mod -= 1;
}
if (neg)
sz_mod -= 1;
while (length < sz_mod)
buf[length++] = '0';
}
if (alternate_form)
{
if (base == 8)
buf[length++] = '0';
if (base == 16) {
buf[length++] = big ? 'X' : 'x';
buf[length++] = '0';
}
}
if (neg)
buf[length++] = '-';
else {
if (sign == '+')
buf[length++] = '+';
else if (sign == ' ')
buf[length++] = ' ';
}
for (int i=0; i < length/2; i++)
{
char tmp = buf[i];
buf[i] = buf[length - i - 1];
buf[length - i - 1] = tmp;
}
buf[length] = 0;
}
void vkprintf_pc(putc_func putc_f, void *putc_data, const char * restrict format, va_list args)
{
char tmpbuf[32];
while(*format)
{
char c;
char alternate_form = 0;
int size_mod = 0;
int length_mod = 0;
int precision = 0;
char zero_pad = 0;
char *str;
char sign = 0;
char leftalign = 0;
uintptr_t value = 0;
intptr_t ivalue = 0;
char big = 0;
c = *format++;
if (c != '%')
{
putc_f(putc_data, c);
}
else
{
c = *format++;
if (c == '#') {
alternate_form = 1;
c = *format++;
}
if (c == '-') {
leftalign = 1;
c = *format++;
}
if (c == ' ' || c == '+') {
sign = c;
c = *format++;
}
if (c == '0') {
zero_pad = 1;
c = *format++;
}
while(c >= '0' && c <= '9') {
size_mod = size_mod * 10;
size_mod = size_mod + c - '0';
c = *format++;
}
if (c == '.') {
c = *format++;
while(c >= '0' && c <= '9') {
precision = precision * 10;
precision = precision + c - '0';
c = *format++;
}
}
big = 0;
if (c == 'h')
{
c = *format++;
if (c == 'h')
{
c = *format++;
length_mod = 1;
}
else length_mod = 2;
}
else if (c == 'l')
{
c = *format++;
if (c == 'l')
{
c = *format++;
length_mod = 8;
}
else length_mod = 4;
}
else if (c == 'j')
{
c = *format++;
length_mod = 9;
}
else if (c == 't')
{
c = *format++;
length_mod = 10;
}
else if (c == 'z')
{
c = *format++;
length_mod = 11;
}
switch (c) {
case 0:
return;
case '%':
putc_f(putc_data, '%');
break;
case 'X':
big = 1;
/* fallthrough */
case 'x':
switch (length_mod) {
case 8:
value = va_arg(args, uint64_t);
break;
case 9:
value = va_arg(args, uintmax_t);
break;
case 10:
value = va_arg(args, uintptr_t);
break;
case 11:
value = va_arg(args, size_t);
break;
default:
value = va_arg(args, unsigned int);
break;
}
int_itoa(tmpbuf, 16, value, zero_pad, precision, size_mod, big, alternate_form, 0, sign);
str = tmpbuf;
size_mod -= int_strlen(str);
if (!leftalign)
while(size_mod-- > 0)
putc_f(putc_data, ' ');
do {
putc_f(putc_data, *str);
} while(*str++);
if (leftalign)
while(size_mod-- > 0)
putc_f(putc_data, ' ');
break;
case 'u':
switch (length_mod) {
case 8:
value = va_arg(args, uint64_t);
break;
case 9:
value = va_arg(args, uintmax_t);
break;
case 10:
value = va_arg(args, uintptr_t);
break;
case 11:
value = va_arg(args, size_t);
break;
default:
value = va_arg(args, unsigned int);
break;
}
int_itoa(tmpbuf, 10, value, zero_pad, precision, size_mod, 0, alternate_form, 0, sign);
str = tmpbuf;
size_mod -= int_strlen(str);
if (!leftalign)
while(size_mod-- > 0)
putc_f(putc_data, ' ');
do {
putc_f(putc_data, *str);
} while(*str++);
if (leftalign)
while(size_mod-- > 0)
putc_f(putc_data, ' ');
break;
case 'd':
case 'i':
switch (length_mod) {
case 8:
ivalue = va_arg(args, int64_t);
break;
case 9:
ivalue = va_arg(args, intmax_t);
break;
case 10:
ivalue = va_arg(args, intptr_t);
break;
case 11:
ivalue = va_arg(args, size_t);
break;
default:
ivalue = va_arg(args, int);
break;
}
if (ivalue < 0)
int_itoa(tmpbuf, 10, -ivalue, zero_pad, precision, size_mod, 0, alternate_form, 1, sign);
else
int_itoa(tmpbuf, 10, ivalue, zero_pad, precision, size_mod, 0, alternate_form, 0, sign);
str = tmpbuf;
size_mod -= int_strlen(str);
if (!leftalign)
while(size_mod-- > 0)
putc_f(putc_data, ' ');
do {
putc_f(putc_data, *str);
} while(*str++);
if (leftalign)
while(size_mod-- > 0)
putc_f(putc_data, ' ');
break;
case 'o':
switch (length_mod) {
case 8:
value = va_arg(args, uint64_t);
break;
case 9:
value = va_arg(args, uintmax_t);
break;
case 10:
value = va_arg(args, uintptr_t);
break;
case 11:
value = va_arg(args, size_t);
break;
default:
value = va_arg(args, uint32_t);
break;
}
int_itoa(tmpbuf, 8, value, zero_pad, precision, size_mod, 0, alternate_form, 0, sign);
str = tmpbuf;
size_mod -= int_strlen(str);
if (!leftalign)
while(size_mod-- > 0)
putc_f(putc_data, ' ');
do {
putc_f(putc_data, *str);
} while(*str++);
if (leftalign)
while(size_mod-- > 0)
putc_f(putc_data, ' ');
break;
case 'c':
putc_f(putc_data, va_arg(args, int));
break;
case 's':
{
str = va_arg(args, char *);
do {
if (*str == 0)
break;
else
putc_f(putc_data, *str);
} while(*str++ && --precision);
}
break;
default:
putc_f(putc_data, c);
break;
}
}
}
}
#define ARM_PERIIOBASE ((uint32_t)io_base)
void waitSerOUT(void *io_base)
{
while(1)
{
if ((rd32le(PL011_0_BASE + PL011_FR) & PL011_FR_TXFF) == 0) break;
}
}
void putByte(void *io_base, char chr)
{
waitSerOUT(io_base);
if (chr == '\n')
{
wr32le(PL011_0_BASE + PL011_DR, '\r');
waitSerOUT(io_base);
}
wr32le(PL011_0_BASE + PL011_DR, (uint8_t)chr);
}
void kprintf_pc(putc_func putc_f, void *putc_data, const char * restrict format, ...)
{
va_list v;
va_start(v, format);
vkprintf_pc(putc_f, putc_data, format, v);
va_end(v);
}
void kprintf(const char * restrict format, ...)
{
va_list v;
va_start(v, format);
vkprintf_pc(putByte, (void*)0xf2000000, format, v);
va_end(v);
}
void vkprintf(const char * restrict format, va_list args)
{
vkprintf_pc(putByte, (void*)0xf2000000, format, args);
}
char b[100];
void start()
{
kprintf(hello_msg);
/*for (int i=0; i < 20; i++)
b[i] = '.';
b[20] = 0;
int_itoa(b, 10, 1979, 0, 0, 0, 0, 0, 0, 0);
kprintf(b);*/
uint8_t *ptr = __boot;
kprintf("[M68K] Writing decimal: %d, writing hex: %x\n", 1536, 1536);
kprintf("[M68K] code resides at %08x, dumping few bytes of it:", __boot);
for (int i=0; i < 64; i++)
{
if (i % 8 == 0)
kprintf("\n[M68k]");
kprintf(" %02x", ptr[i]);
}
kprintf("\n[M68K] done :-D\n");
}