-
Notifications
You must be signed in to change notification settings - Fork 10
/
emupdk.c
85 lines (69 loc) · 1.81 KB
/
emupdk.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
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#include "emucpu.h"
void emuException(struct emuCPU *cpu, int code)
{
printf( "EXCEPTION: %d @0x%04x\n\n", ePC, code );
exit(0);
}
int main( int argc, const char * argv [] )
{
if( (argc<2) || (argc>3) ) {
printf("usage: %s [otpidhex] inputfile\n"
"example: %s input.pdk\n"
"example: %s 2AA1 input.bin\n"
"example: %s 2AA1 input.ihex\n",
argv[0], argv[0], argv[0], argv[0]);
return 0;
}
struct emuCPU ecpu;
struct emuCPU* cpu = &ecpu;
if( 3 == argc )
{
uint16_t otp_id;
if( 1 != sscanf(argv[1], "%" SCNx16, &otp_id) )
{
printf("Error wrong otp id: %s\n",argv[1]);
return -1;
}
if( (emuCPUloadIHEX(cpu, argv[2], otp_id) < 0) &&
(emuCPUloadBIN(cpu, argv[2], false, otp_id) < 0) ) {
printf("Error reading input file\n");
return -1;
}
}
else
{
if( emuCPUloadPDK(cpu, argv[1], true) < 0 ) {
printf("Error reading input file\n");
return -1;
}
}
if( !cpu->fnReset || !cpu->fnExecute ) {
printf("Missing emulator for this CPU\n");
return -2;
}
//set our own emulation exception handler
cpu->fnException = emuException;
//reset CPU
cpu->fnReset(cpu,true);
for( uint32_t i=0;i<2000;i++ )
{
char disass[64] = "";
if( cpu->fnDisassemble )
cpu->fnDisassemble(cpu,ePC,disass);
printf( "C:%10d A=0x%02x F=0x%02x SP=0x%02x PC=0x%04x: 0x%04x %s\n", eCycle, eA, eF, eSP, ePC, cpu->eCode[ePC]&0x3FFF, disass );
uint32_t oldPC = ePC;
int rc = cpu->fnExecute(cpu);
//todo: simulate peripherals
if( 0 != rc )
break;
//detect endless loop and stop
if( oldPC == ePC )
break;
}
return 0;
}