/
main.cpp
executable file
·125 lines (96 loc) · 3.39 KB
/
main.cpp
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
/*
MSP430 Emulator
Copyright (C) 2022 Rudolf Geosits (rgeosits@live.esu.edu)
"MSP430 Emulator" is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
"MSP430 Emulator" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "main.h"
int main(int argc, char *argv[])
{
Emulator *emu = (Emulator *) calloc( 1, sizeof(Emulator) );
Cpu *cpu = NULL; Debugger *deb = NULL;
emu->cpu = (Cpu *) calloc(1, sizeof(Cpu));
emu->cpu->bcm = (Bcm *) calloc(1, sizeof(Bcm));
emu->cpu->timer_a = (Timer_a *) calloc(1, sizeof(Timer_a));
emu->cpu->p1 = (Port_1 *) calloc(1, sizeof(Port_1));
emu->cpu->usci = (Usci *) calloc(1, sizeof(Usci));
emu->debugger = (Debugger *) calloc(1, sizeof(Debugger));
setup_debugger(emu);
cpu = emu->cpu;
deb = emu->debugger;
deb->server = (Server *) calloc(1, sizeof(Server));
if (deb->web_interface == true)
{
if (argv[1] == NULL)
{
puts("Need port argument");
return(1);
}
deb->ws_port = strtoul(argv[1], NULL, 10);
pthread_t *t = &deb->web_server_thread;
if ( pthread_create(t, NULL, web_server, (void *)emu ) )
{
fprintf(stderr, "Error creating web server thread\n");
return 1;
}
while (!deb->web_server_ready)
usleep(10000);
print_console(emu, " [MSP430 Emulator]\n Copyright (C) 2022 Rudolf Geosits (rgeosits@live.esu.edu)\n");
print_console(emu, " [!] Upload your firmware (ELF format only); Type 'h' for debugger options.\n\n");
while (!deb->web_firmware_uploaded)
usleep(10000);
}
//register_signal(SIGINT); // Register Callback for CONTROL-c
initialize_msp_memspace();
initialize_msp_registers(emu);
setup_bcm(emu);
setup_timer_a(emu);
setup_port_1(emu);
setup_usci(emu);
load_bootloader(0x0C00);
if (deb->web_interface)
load_firmware(emu, (char*)"tmp.bin", 0xC000);
// display first round of registers
display_registers(emu);
disassemble(emu, cpu->pc, 1);
update_register_display(emu);
// Fetch-Decode-Execute Cycle (run machine)
while (!deb->quit)
{
// Handle debugger when CPU is not running
if (!cpu->running)
{
usleep(10000);
continue;
}
// Handle Breakpoints
handle_breakpoints(emu);
// Instruction Decoder
decode(emu, fetch(emu), EXECUTE);
// Handle Peripherals
handle_bcm(emu);
handle_timer_a(emu);
handle_port_1(emu);
handle_usci(emu);
// Average of 4 cycles per instruction
mclk_wait_cycles(emu, 4);
}
uninitialize_msp_memspace();
free(cpu->timer_a);
free(cpu->bcm);
free(cpu->p1);
free(cpu->usci);
free(cpu);
free(deb->server);
free(deb);
free(emu);
return 0;
}