-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconsole_imgui.cpp
More file actions
357 lines (302 loc) · 11.3 KB
/
Copy pathconsole_imgui.cpp
File metadata and controls
357 lines (302 loc) · 11.3 KB
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
// (C) 2026 by Folkert van Heusden
// Released under MIT license
#if defined(USE_IMGUI)
#include "gen.h"
#include <atomic>
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
#include "imgui/imgui.h"
#include "imgui/backends/imgui_impl_sdl3.h"
#include "imgui/backends/imgui_impl_sdlrenderer3.h"
#include "bus.h"
#include "console_imgui.h"
#include "cpu.h"
#define GRAPH_N 250
extern aint term_cols;
extern aint term_lines;
console_imgui::console_imgui(std::atomic_uint32_t *const stop_event): console(stop_event)
{
loads.resize(GRAPH_N);
}
console_imgui::~console_imgui()
{
}
void console_imgui::begin()
{
}
int console_imgui::wait_for_char_ll(const int timeout)
{
auto rc = kb_buffer.pop(timeout);
if (rc.has_value() == false)
return -1;
return rc.value();
}
void console_imgui::put_char_ll(const char)
{
refresh = true;
}
void console_imgui::put_string_lf(const std::string & what)
{
put_string(what + "\r\n");
}
void console_imgui::resize_terminal()
{
refresh = true;
}
void console_imgui::panel_update_thread()
{
set_thread_name("panel");
constexpr const auto pixel_format = SDL_PIXELFORMAT_ARGB8888;
uint32_t c_red_bright = 0;
uint32_t c_red_dim = 0;
cpu *const c = b->getCpu();
TTF_Init();
TTF_Font *font = TTF_OpenFont("/usr/share/vlc/skins2/fonts/FreeSans.ttf", 32);
SDL_Color white { 255, 255, 255, 255 };
SDL_Surface *text_address = TTF_RenderText_Blended(font, "address", 0, white);
SDL_Surface *text_instruction = TTF_RenderText_Blended(font, "instr.", 0, white);
SDL_Surface *text_psw = TTF_RenderText_Blended(font, "psw", 0, white);
SDL_Surface *text_disk_read = TTF_RenderText_Blended(font, "disk read", 0, white);
SDL_Surface *text_disk_write = TTF_RenderText_Blended(font, "disk write", 0, white);
SDL_Surface *text_network = TTF_RenderText_Blended(font, "network", 0, white);
SDL_Surface *text_mmu_ena = TTF_RenderText_Blended(font, "mmu enabl.", 0, white);
uint64_t prev_instr_count = c->get_instructions_executed_count();
while(!stop && *stop_event != EVENT_TERMINATE) {
if (panel_w <= 0) {
SDL_Delay(100);
continue;
}
{
uint64_t cur_instr_count = c->get_instructions_executed_count();
uint64_t done_n_instr = prev_instr_count <= cur_instr_count ? cur_instr_count - prev_instr_count : 0;
my_unique_lock lck(&loads_lock);
while(loads.size() >= GRAPH_N)
loads.erase(loads.begin());
loads.push_back(done_n_instr);
prev_instr_count = cur_instr_count;
}
SDL_Surface *new_surface = nullptr;
{
my_unique_lock lck(&panel_lock);
new_surface = SDL_CreateSurface(panel_w, panel_h, pixel_format);
}
const SDL_PixelFormatDetails *format = SDL_GetPixelFormatDetails(new_surface->format);
c_red_bright = SDL_MapRGB(format, nullptr, 255, 0, 0);
c_red_dim = SDL_MapRGB(format, nullptr, 16, 0, 0);
uint16_t current_PSW = c->getPSW();
int run_mode = current_PSW >> 14;
uint16_t current_PC = c->getPC();
memory_addresses_t rc = b->getMMU()->calculate_physical_address(run_mode, current_PC);
auto current_instr = b->peek_word(run_mode, current_PC);
int pix_w = new_surface->w * 80 / 100;
int text_w = new_surface->w * 20 / 100;
int led_d = pix_w / 22;
int text_h = pix_w / 26;
// address
for(int i=0; i<22; i++) {
SDL_Rect rect { 0 + led_d * i, 0, led_d, led_d };
SDL_FillSurfaceRect(new_surface, &rect, rc.physical_instruction & (1 << (21 - i)) ? c_red_bright : c_red_dim);
}
SDL_Rect text_address_to { 23 * led_d, 0, text_w, text_h };
SDL_BlitSurface(text_address, nullptr, new_surface, &text_address_to );
// data
if (current_instr.has_value()) {
for(int i=0; i<16; i++) {
SDL_Rect rect { 0 + led_d * i, led_d, led_d, led_d };
SDL_FillSurfaceRect(new_surface, &rect, current_instr.value() & (1 << (15 - i)) ? c_red_bright : c_red_dim);
}
}
SDL_Rect text_instruction_to { 23 * led_d, led_d, text_w, text_h };
SDL_BlitSurface(text_instruction, nullptr, new_surface, &text_instruction_to);
// PSW
for(int i=0; i<16; i++) {
SDL_Rect rect { 0 + led_d * i, led_d * 2, led_d, led_d };
SDL_FillSurfaceRect(new_surface, &rect, current_PSW & (1 << (15 - i)) ? c_red_bright : c_red_dim);
}
SDL_Rect text_psw_to { 23 * led_d, led_d * 2, text_w, text_h };
SDL_BlitSurface(text_psw, nullptr, new_surface, &text_psw_to);
// activity LEDs
SDL_Rect rect_dr { 0 + 0, led_d * 3, led_d, led_d };
SDL_FillSurfaceRect(new_surface, &rect_dr, disk_read_activity_flag ? c_red_bright : c_red_dim);
disk_read_activity_flag = false;
SDL_Rect text_disk_read_to { 23 * led_d, led_d * 3, text_w, text_h };
SDL_BlitSurface(text_disk_read, nullptr, new_surface, &text_disk_read_to);
//
SDL_Rect rect_dw { 0 + 0, led_d * 4, led_d, led_d };
SDL_FillSurfaceRect(new_surface, &rect_dw, disk_write_activity_flag ? c_red_bright : c_red_dim);
disk_write_activity_flag = false;
SDL_Rect text_disk_write_to { 23 * led_d, led_d * 4, text_w, text_h };
SDL_BlitSurface(text_disk_write, nullptr, new_surface, &text_disk_write_to);
//
SDL_Rect rect_n { 0 + 0, led_d * 5, led_d, led_d };
SDL_FillSurfaceRect(new_surface, &rect_n, network_activity_flag ? c_red_bright : c_red_dim);
network_activity_flag = false;
SDL_Rect text_network_to { 23 * led_d, led_d * 5, text_w, text_h };
SDL_BlitSurface(text_network, nullptr, new_surface, &text_network_to);
// MMU
SDL_Rect rect_mmu_ena { 0 + 0, led_d * 6, led_d, led_d };
SDL_FillSurfaceRect(new_surface, &rect_mmu_ena, b->getMMU()->is_enabled() ? c_red_bright : c_red_dim);
SDL_Rect text_mmu_ena_to { 23 * led_d, led_d * 6, text_w, text_h };
SDL_BlitSurface(text_mmu_ena, nullptr, new_surface, &text_mmu_ena_to);
{
my_unique_lock lck(&panel_lock);
SDL_DestroySurface(panel);
panel = new_surface;
}
refresh = true;
SDL_Delay(1000 / refreshrate);
}
my_unique_lock lck(&panel_lock);
SDL_DestroySurface(panel);
panel = nullptr;
SDL_DestroySurface(text_address );
SDL_DestroySurface(text_instruction);
SDL_DestroySurface(text_psw );
SDL_DestroySurface(text_disk_read );
SDL_DestroySurface(text_disk_write );
SDL_DestroySurface(text_network );
SDL_DestroySurface(text_mmu_ena );
TTF_CloseFont(font);
}
void console_imgui::refresh_virtual_terminal()
{
refresh = true;
}
void console_imgui::ui_event_loop()
{
set_thread_name("imgui");
if (!SDL_Init(SDL_INIT_VIDEO)) {
printf("Error: SDL_Init(): %s\n", SDL_GetError());
return;
}
float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
SDL_WindowFlags window_flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY;
SDL_Window *window = SDL_CreateWindow("KEK", int(1280 * main_scale), int(800 * main_scale), window_flags);
if (window == nullptr) {
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
return;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, nullptr);
if (renderer == nullptr) {
SDL_Log("Error: SDL_CreateRenderer(): %s\n", SDL_GetError());
return;
}
// SDL_SetRenderVSync(renderer, 1);
SDL_ShowWindow(window);
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.Fonts->AddFontDefaultVector();
ImGui::StyleColorsDark();
ImGuiStyle& style = ImGui::GetStyle();
style.ScaleAllSizes(main_scale);
style.FontScaleDpi = main_scale;
// Setup Platform/Renderer backends
ImGui_ImplSDL3_InitForSDLRenderer(window, renderer);
ImGui_ImplSDLRenderer3_Init(renderer);
while(!stop && *stop_event != EVENT_TERMINATE) {
bool minimized = SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED;
SDL_Event event;
while (SDL_WaitEventTimeout(&event, minimized ? 10 : 1)) {
ImGui_ImplSDL3_ProcessEvent(&event);
if (event.type == SDL_EVENT_QUIT)
stop = true;
if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window))
stop = true;
if (event.type == SDL_EVENT_KEY_DOWN) {
SDL_Keycode keycode = SDL_GetKeyFromScancode(event.key.scancode, event.key.mod, false);
if (event.key.mod & SDL_KMOD_CTRL)
kb_buffer.push(toupper(keycode & 0xff) - 'A' + 1);
else if (keycode < 127)
kb_buffer.push(keycode);
}
}
if (refresh.exchange(false) == false)
continue;
ImGui_ImplSDLRenderer3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
///
ImGui::SetNextWindowSize(ImVec2(640.0f, 320.0f), ImGuiCond_FirstUseEver);
ImGui::Begin("Front panel");
auto available_space = ImGui::GetContentRegionAvail();
SDL_Texture *texture = nullptr;
{
my_unique_lock lck(&panel_lock);
texture = panel ? SDL_CreateTextureFromSurface(renderer, panel) : nullptr;
panel_w = available_space.x;
panel_h = available_space.y;
}
if (texture)
ImGui::Image(ImTextureID(intptr_t(texture)), ImVec2(float(texture->w), float(texture->h)));
ImGui::End();
ImGui::SetNextWindowSize(ImVec2(250.0f, 250.0f), ImGuiCond_FirstUseEver);
ImGui::Begin("System load");
ImVec2 pos = ImGui::GetCursorScreenPos();
ImVec2 dim = ImGui::GetWindowSize();
{
uint64_t max_count = 0;
my_unique_lock lck(&loads_lock);
for(auto element : loads)
max_count = std::max(max_count, element);
if (max_count > 0) {
auto n_elem = loads.size();
float prev_x = 0;
float prev_y = pos.y + dim.y;
for(size_t i=0; i<n_elem; i++) {
float x = pos.x + dim.x * i / n_elem;
float y = pos.y + dim.y - dim.y * loads[i] / max_count;
ImGui::GetWindowDrawList()->AddLine(
{ prev_x, prev_y }, { x, y },
IM_COL32(255, 0, 0, 255),
2.f);
prev_x = x;
prev_y = y;
}
}
}
ImGui::End();
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_FirstUseEver, ImVec2(0.5f, 0.5f));
ImGui::Begin("Terminal");
ImVec2 cpos = ImGui::GetCursorScreenPos();
ImVec2 cdim = ImGui::GetWindowSize();
char *buffer_copy = new char[t_width * t_height];
for(int i=0; i<t_width * t_height; i++)
buffer_copy[i] = screen_buffer[i] ? screen_buffer[i] : ' ';
for(int y=0; y<t_height; y++) {
auto offset = y * t_width;
ImGui::TextUnformatted(&buffer_copy[offset], &buffer_copy[offset + t_width]);
}
delete [] buffer_copy;
// cursor
const float char_w = ImGui::CalcTextSize("M").x;
const float char_h = ImGui::GetTextLineHeightWithSpacing();
const float cursor_h = ImGui::GetTextLineHeight();
float x = cpos.x + tx * char_w;
float y = cpos.y + ty * char_h;
ImGui::GetWindowDrawList()->AddLine({ x, y + cursor_h }, { x + char_w, y + cursor_h }, IM_COL32(255, 255, 0, 127), 2.f);
ImGui::End();
// for help
term_cols = cdim.x / (char_w + 1.);
term_lines = cdim.y / char_h;
// Rendering
ImGui::Render();
SDL_SetRenderScale(renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
SDL_RenderClear(renderer);
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), renderer);
SDL_RenderPresent(renderer);
if (texture)
SDL_DestroyTexture(texture);
}
*stop_event = EVENT_TERMINATE;
ImGui_ImplSDLRenderer3_Shutdown();
ImGui_ImplSDL3_Shutdown();
ImGui::DestroyContext();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
#endif