-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtamalib.c
171 lines (141 loc) · 3.3 KB
/
tamalib.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
/*
* TamaLIB - A hardware agnostic Tamagotchi P1 emulation library
*
* Copyright (C) 2021 Jean-Christophe Rona <jc@rona.fr>
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "tamalib.h"
#include "hw.h"
#include "cpu.h"
#include "hal.h"
#define DEFAULT_FRAMERATE 3// fps
static exec_mode_t exec_mode = EXEC_MODE_RUN;
static u32_t step_depth = 0;
static timestamp_t screen_ts = 0;
static u32_t ts_freq;
static u8_t g_framerate = DEFAULT_FRAMERATE;
hal_t *g_hal;
bool_t tamalib_init(u32_t freq)
//bool_t tamalib_init(breakpoint_t *breakpoints, u32_t freq)
{
bool_t res = 0;
res |= cpu_init( freq);
// res |= cpu_init(program, breakpoints, freq);
res |= hw_init();
ts_freq = freq;
return res;
}
/*
void tamalib_release(void)
{
hw_release();
cpu_release();
}*/
void tamalib_set_framerate(u8_t framerate)
{
g_framerate = framerate;
}
/*
u8_t tamalib_get_framerate(void)
{
//return g_framerate;
return DEFAULT_FRAMERATE;
}
*/
void tamalib_register_hal(hal_t *hal)
{
g_hal = hal;
}
/*
void tamalib_set_exec_mode(exec_mode_t mode)
{
exec_mode = mode;
step_depth = cpu_get_depth();
cpu_sync_ref_timestamp();
}
*/
/*
void tamalib_step(void)
{
if (exec_mode == EXEC_MODE_PAUSE) {
return;
}
if (cpu_step()) {
exec_mode = EXEC_MODE_PAUSE;
step_depth = cpu_get_depth();
} else {
switch (exec_mode) {
case EXEC_MODE_PAUSE:
case EXEC_MODE_RUN:
break;
case EXEC_MODE_STEP:
exec_mode = EXEC_MODE_PAUSE;
break;
case EXEC_MODE_NEXT:
if (cpu_get_depth() <= step_depth) {
exec_mode = EXEC_MODE_PAUSE;
step_depth = cpu_get_depth();
}
break;
case EXEC_MODE_TO_CALL:
if (cpu_get_depth() > step_depth) {
exec_mode = EXEC_MODE_PAUSE;
step_depth = cpu_get_depth();
}
break;
case EXEC_MODE_TO_RET:
if (cpu_get_depth() < step_depth) {
exec_mode = EXEC_MODE_PAUSE;
step_depth = cpu_get_depth();
}
break;
}
}
}
*/
/*
void tamalib_mainloop(void)
{
timestamp_t ts;
while (!g_hal->handler()) {
tamalib_step();
ts = g_hal->get_timestamp();
if (ts - screen_ts >= ts_freq/g_framerate) {
screen_ts = ts;
g_hal->update_screen();
}
}
} */
void tamalib_mainloop_step_by_step(void)
{
timestamp_t ts;
if (!g_hal->handler()) {
//tamalib_step();
if (exec_mode == EXEC_MODE_RUN) {
if (cpu_step()) {
exec_mode = EXEC_MODE_PAUSE;
step_depth = cpu_get_depth();
}
}
/* Update the screen @ g_framerate fps */
ts = g_hal->get_timestamp();
if (ts - screen_ts >= ts_freq/g_framerate) {
//if (ts - screen_ts >= ts_freq/DEFAULT_FRAMERATE) {
screen_ts = ts;
g_hal->update_screen();
}
}
}