Removing TODO related to freeing X-related allocations. The only things we aren't...
[screensavers] / hacks / NEDsim / NEDsim.c
CommitLineData
84b74595
AT
1/* (c) 2021 Aaron Taylor <ataylor at subgeniuskitty dot com> */
2/* See LICENSE.txt file for copyright and license details. */
3
84b74595
AT
4#include "screenhack.h"
5#include "simulator.h"
6
b1906d46
AT
7// TODO: What should I do about this define? I would like to be able to specify the address so I can do things like display the code itself if nothign interesting happens in RAM.
8#define HEAP_START_ADDRESS 0x20000000
9
28edd14e
AT
10/* -------------------------------------------------------------------------- */
11/* Front Panel Layout */
12/* -------------------------------------------------------------------------- */
13
14// All of these values are in units of 'cells', not 'pixels'. Where applicable,
15// coordinates refer to the upper-left corner of a cell. See the files
16// `layout_reference.*` for details.
17
18#define OVERALL_WIDTH_IN_CELLS 70
19#define HEADER_HEIGHT_IN_CELLS 14
20#define FOOTER_HEIGHT_IN_CELLS 2
21
22#define LOGO_X_OFFSET 2
23#define LOGO_Y_OFFSET 2
24#define LOGO_WIDTH 20
25#define LOGO_NAME_HEIGHT 6
26#define LOGO_WEBSITE_HEIGHT 2
27
28#define HALT_X_OFFSET 26
29#define HALT_Y_OFFSET 2
30#define HALT_WIDTH 6
31#define HALT_LIGHT_HEIGHT 6
32#define HALT_LABEL_HEIGHT 2
33
34#define WORDLINE_BITS_PER_STRIPE 4
35#define WORDLINE_WIDTH 32
36#define WORDLINE_HEIGHT 1
37
38#define PC_X_OFFSET 36
39#define PC_Y_OFFSET 7
40#define PC_WIDTH 32
41#define PC_LABEL_HEIGHT 2
42#define PC_LIGHT_HEIGHT 1
43
44#define SC_X_OFFSET 42
45#define SC_Y_OFFSET 2
46#define SC_WIDTH 5
47#define SC_LABEL_HEIGHT 2
48#define SC_LIGHT_HEIGHT 1
49
50#define PSW_N_X_OFFSET 51
51#define PSW_Z_X_OFFSET 58
52#define PSW_Y_OFFSET 2
53#define PSW_LABEL_WIDTH 3
54#define PSW_LABEL_HEIGHT 2
55#define PSW_LIGHT_WIDTH 1
56#define PSW_LIGHT_HEIGHT 1
57
58#define STACK_X_OFFSET 2
59#define STACK_Y_OFFSET 12
60#define STACK_WIDTH 32
61#define STACK_LABEL_HEIGHT 2
62#define STACK_LIGHT_HEIGHT 1
63
64#define HEAP_X_OFFSET 36
65#define HEAP_Y_OFFSET 12
66#define HEAP_WIDTH 32
67#define HEAP_LABEL_HEIGHT 2
68#define HEAP_LIGHT_HEIGHT 1
69
84b74595
AT
70/* -------------------------------------------------------------------------- */
71/* Data Structures */
72/* -------------------------------------------------------------------------- */
73
74struct NEDsim {
f93fc861 75 // Various X resources
84b74595
AT
76 Display * dpy;
77 Window win;
78 GC gc;
79
f93fc861 80 // Tracks the width and height (in pixels) of 'win' on 'dpy'.
84b74595
AT
81 int dpy_width, dpy_height;
82
f93fc861
AT
83 // To ensure the NED display is always synchronized/valid, we
84 // double-buffer. This is that buffer.
84b74595
AT
85 Pixmap panel;
86
f93fc861
AT
87 // The front panel is defined in a fixed grid of 'cells' which are then
88 // scaled and projected onto the actual screen. This variable tracks the
89 // size of one 'cell' in screen pixels.
84b74595 90 int cell_size;
f93fc861
AT
91 // Like above, this variable tracks the scaled size (in pixels) of cell
92 // borders when projecting them on the screen.
84b74595 93 int border_size;
f93fc861
AT
94
95 // Since the panel will be smaller than the display window, these two
96 // variable track the location (in pixels) of the upper left corner of the
97 // NED panel (pretending it didn't have rounded corners) relative to the
98 // display window's origin.
84b74595 99 int origin_x_offset, origin_y_offset;
f93fc861
AT
100
101 // Delay (in microseconds) between clock cycles in the NED CPU.
102 size_t delay;
103
104 // Since the heap and stack areas are resized to fit the display, this
105 // variable tracks their height.
84b74595
AT
106 int num_data_rows;
107
f93fc861 108 // This is an index into the color_list[] array, selecting a color scheme.
84b74595
AT
109 size_t color_index;
110
f93fc861
AT
111 // If the display is unsuitable (e.g. too small), this boolean is 'true'.
112 // With this, we blank the display but keep the simulation itself running,
113 // allowing the ongoing simulation to display when the situation is
114 // corrected, and allowing us to avoid boring the user with unnecessary
115 // simulation restarts.
84b74595
AT
116 Bool suitable_display;
117
f93fc861
AT
118 // The font size is dynamically adjusted based on display size. This
119 // variable stores the full font description, including the correct font
120 // size, all in the X11 font specifier format. For example,
121 // -*-helvetica-*-r-*-*-72-*-*-*-*-*-*-*
122 // if the display were using a 72 point helvetica font without emphasis.
84b74595
AT
123 char * current_font;
124
f93fc861 125 // This struct contains all machine state for the simulated NED computer.
84b74595
AT
126 struct NEDstate * nedstate;
127};
128
129struct color_rgb {
f93fc861
AT
130 // The type 'unsigned short' comes from the XColor struct definition.
131 // The red, green, and blue values are always in the range 0 to 65535
132 // inclusive, independent of the number of bits actually used in the
133 // display hardware. The server scales these values to the range used
134 // by the hardware. Black is represented by (0,0,0), and white is
135 // represented by (65535,65535,65535).
84b74595
AT
136 unsigned short red, green, blue;
137};
138
139struct color_scheme {
f93fc861 140 // The following entries define a full color scheme for a NED panel.
84b74595 141 struct color_rgb
f93fc861
AT
142 panel_bg, // Empty screen space not taken up by the panel
143 panel_fg, // Panel body/faceplate
144 light_on, // Illuminated data indicator
145 light_off, // Non-illuminated data indicator
146 error_on, // Illuminated error indicator
147 error_off, // Non-illuminated error indicator
148 primary, // Primary panel color (see: logo background)
149 secondary, // Secondary panel color (see: "HALT" text background)
150 tertiary, // Tertiary panel color (see: "subgeniuskitty.com" text background)
151 border, // Cell borders
152 text; // Panel text
84b74595
AT
153};
154
f93fc861
AT
155// All included color schemes for NEDsim are defined in this array. To select a
156// particular color scheme at runtime, use the `-color N` CLI option.
84b74595 157static struct color_scheme color_list[] = {
f93fc861
AT
158 { // This color scheme is inspired by the KI10 version of the PDP-10.
159 // Many RGB values came from: http://www.chdickman.com/pdp8/DECcolors/
84b74595
AT
160 {63479,63479,63479}, // 092-XXXX-123
161 { 5140, 2056, 5654}, // 092-XXXX-152
f93fc861
AT
162 {65535,11051,15677}, // homemade, derived from 092-XXXX-139
163 {20000,13107,12850}, // homemade, derived from 092-XXXX-154
164 {65535,13107,12850}, // homemade, derived from 092-XXXX-154
165 {20000,13107,12850}, // homemade, derived from 092-XXXX-154
84b74595
AT
166 { 4112,12850,20046}, // 092-XXXX-145
167 {12336,29555,37008}, // 092-XXXX-151
168 {30326,24158, 6425}, // 092-XXXX-157
169 {63479,63479,63479}, // 092-XXXX-123
170 {63479,63479,63479} // 092-XXXX-123
f93fc861
AT
171 },
172 { // This color scheme is (very loosely) inspired by the first version of the PDP-11/70.
173 // Many RGB values came from: http://www.chdickman.com/pdp8/DECcolors/
174 {63479,63479,63479}, // 092-XXXX-123
175 { 5140, 2056, 5654}, // 092-XXXX-152
176 {51400,34952,26682}, // homemade, derived from 092-XXXX-136
177 {20000,13107,12850}, // homemade, derived from 092-XXXX-154
178 {65535,13107,12850}, // homemade, derived from 092-XXXX-154
179 {20000,13107,12850}, // homemade, derived from 092-XXXX-154
180 {28270, 8995,19532}, // 092-XXXX-140
181 {40092,11051,15677}, // 092-XXXX-139
182 {15000,21000, 4000}, // homemade, derived from 092-XXXX-129
183 {63479,63479,63479}, // 092-XXXX-123
184 {63479,63479,63479} // 092-XXXX-123
84b74595
AT
185 }
186};
187
188/* -------------------------------------------------------------------------- */
189/* Helper Functions */
190/* -------------------------------------------------------------------------- */
191
7361f6db
AT
192// Fill in boilerplate when selecting colors.
193#define COLOR(X) &color_list[nedsim->color_index].X
194
280460e6 195// Set foreground color for the current graphics context.
84b74595
AT
196static void
197set_color(struct NEDsim * nedsim, struct color_rgb * color)
198{
199 XColor temp;
200 XWindowAttributes xgwa;
201
202 XGetWindowAttributes(nedsim->dpy, nedsim->win, &xgwa);
203
204 temp.red = color->red;
205 temp.green = color->green;
206 temp.blue = color->blue;
207
208 XAllocColor(nedsim->dpy, xgwa.colormap, &temp);
209 XSetForeground(nedsim->dpy, nedsim->gc, temp.pixel);
210}
211
5a772e24
AT
212// Set font size to fill 'size' cells vertically, minus space for border and
213// padding. There is probably a better way to do this. The current method is
214// horribly slow. Call this function as few times as possible.
84b74595
AT
215static void
216set_font_size(struct NEDsim * nedsim, int size)
217{
218 // vvv--- for border ---vvv vvv--- for padding ---vvv
219 int desired_height_in_pixels = (size * nedsim->cell_size) - (2 * nedsim->border_size) - (8 * nedsim->border_size);
220
221 const char * font_size_prefix = "-*-helvetica-*-r-*-*-";
222 const char * font_size_suffix = "-*-*-*-*-*-*-*";
223
5a772e24
AT
224 // In the buffer, we reserve 100 DIGITS for the font size integer.
225 size_t buffer_size = strlen(font_size_prefix) + strlen(font_size_suffix) + 100;
84b74595 226 char * font_full_name = malloc(buffer_size);
5a772e24
AT
227 // Start with a 2pt font and work our way up.
228 int font_size_in_points = 2;
84b74595
AT
229
230 while (1) {
231 // Load the font.
232 snprintf(font_full_name, buffer_size, "%s%d%s", font_size_prefix, font_size_in_points, font_size_suffix);
233 XFontStruct * font = XLoadQueryFont(nedsim->dpy, font_full_name);
234 if (!font) {
235 printf("WARNING: Unable to load font %s. Probably gonna look wonky.\n", font_full_name);
236 font = XLoadQueryFont(nedsim->dpy, "fixed");
237 break;
238 }
239 XSetFont(nedsim->dpy, nedsim->gc, font->fid);
240
241 // Get the height.
242 int direction, ascent, descent;
243 XCharStruct overall;
244 XTextExtents(font, "X", 1, &direction, &ascent, &descent, &overall);
245
246 // Compare the height.
247 int height = overall.ascent - overall.descent;
248 if (height == desired_height_in_pixels) {
249 break;
250 } else if (height > desired_height_in_pixels) {
251 font_size_in_points--;
252 snprintf(font_full_name, buffer_size, "%s%d%s", font_size_prefix, font_size_in_points, font_size_suffix);
253 XFontStruct * font = XLoadQueryFont(nedsim->dpy, font_full_name);
254 if (!font) {
255 printf("WARNING: Unable to load font %s. Probably gonna look wonky.\n", font_full_name);
256 font = XLoadQueryFont(nedsim->dpy, "fixed");
257 break;
258 }
259 XSetFont(nedsim->dpy, nedsim->gc, font->fid);
260 break;
261 } else {
262 font_size_in_points++;
263 }
264 }
265
266 free(nedsim->current_font);
267 nedsim->current_font = font_full_name;
268}
269
280460e6
AT
270// Unlike most functions in this program that input/output in units of 'cells',
271// this function uses units of 'pixels' so the caller can track fractional cell
272// usage (e.g. for centering the text).
84b74595
AT
273static void
274get_text_size(struct NEDsim * nedsim, const char * text, int * x_size, int * y_size)
275{
276 int direction, ascent, descent;
277 XCharStruct overall;
278 XFontStruct * font = XLoadQueryFont(nedsim->dpy, nedsim->current_font);
279 XTextExtents(font, text, strlen(text), &direction, &ascent, &descent, &overall);
280 *x_size = overall.width;
281 *y_size = overall.ascent - overall.descent;
282}
283
280460e6 284// When specifying the rectangular area to draw, all coordinates and sizes are
b1906d46 285// in units of 'cells'.
84b74595 286static void
33c35ba0
AT
287draw_rect_area(struct NEDsim * nedsim, struct color_rgb * color,
288 size_t x_origin, size_t y_origin, size_t x_size, size_t y_size,
84b74595
AT
289 Bool bord_top, Bool bord_bottom, Bool bord_left, Bool bord_right)
290{
280460e6 291 // First fill in the rectangular area...
84b74595
AT
292 x_origin *= nedsim->cell_size;
293 x_origin += nedsim->origin_x_offset;
294 y_origin *= nedsim->cell_size;
295 y_origin += nedsim->origin_y_offset;
296 x_size *= nedsim->cell_size;
297 y_size *= nedsim->cell_size;
33c35ba0 298 set_color(nedsim, color);
b1906d46
AT
299 XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
300 x_origin, y_origin, x_size, y_size
301 );
84b74595 302
280460e6 303 // ...then give it a border, if requested.
7361f6db 304 set_color(nedsim, COLOR(border));
84b74595 305 if (bord_top) {
280460e6 306 XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
b1906d46
AT
307 x_origin, y_origin,
308 x_size, nedsim->border_size
309 );
84b74595
AT
310 }
311 if (bord_bottom) {
280460e6 312 XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
b1906d46
AT
313 x_origin, (y_origin + y_size - nedsim->border_size),
314 x_size, nedsim->border_size
315 );
84b74595
AT
316 }
317 if (bord_left) {
280460e6 318 XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
b1906d46
AT
319 x_origin, y_origin,
320 nedsim->border_size, y_size
321 );
84b74595
AT
322 }
323 if (bord_right) {
280460e6 324 XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
b1906d46
AT
325 (x_origin + x_size - nedsim->border_size), y_origin,
326 nedsim->border_size, y_size
327 );
84b74595
AT
328 }
329}
330
280460e6
AT
331// Draws filled circle in a square area with upper left corner at x,y. When
332// specifying the location and size to draw, all values are in units of
b1906d46 333// 'cells'.
84b74595
AT
334static void
335draw_circular_area(struct NEDsim * nedsim, size_t x, size_t y, double diameter)
336{
337 // First, convert the function argument units from 'cells' to 'pixels'
338 x *= nedsim->cell_size;
339 y *= nedsim->cell_size;
340 diameter *= nedsim->cell_size;
341
342 // Add the panel's absolute x,y offset to the requested coordinates.
343 x += nedsim->origin_x_offset;
344 y += nedsim->origin_y_offset;
345
346 // Shrink the circle to be a bit smaller than the bounding box allows. Note
347 // that the three adjustment values must sum to 1.0.
348 // For example, 0.7 + 0.15 + 0.15 = 1.0.
349 x += (0.15 * diameter);
350 y += (0.15 * diameter);
351 diameter *= 0.7;
352
353 // Because we only draw the bottom border on repeated rows (e.g. draw_wordline()),
354 // we need to offset vertically by half a border height.
355 y -= (0.5 * nedsim->border_size);
356
357 // Start angle 0 and ending angle 360*64 is one full circle in Xlib units.
839b757b 358 XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc, x, y, diameter, diameter, 0, 360*64);
84b74595
AT
359}
360
76081fc3
AT
361// Draws text in a square area with upper left corner at (x_origin,y_origin).
362// Requires that set_font_size() has been run at least once. All values are in
363// units of 'cells'.
364static void
b1906d46
AT
365draw_text(struct NEDsim * nedsim, const char * text,
366 int x_origin, int y_origin, int x_size, int y_size, Bool horizontally_center)
76081fc3 367{
7361f6db 368 set_color(nedsim, COLOR(text));
76081fc3
AT
369
370 int text_x_size, text_y_size;
371 get_text_size(nedsim, text, &text_x_size, &text_y_size);
372
373 int local_y_offset = ((y_size * nedsim->cell_size) - text_y_size) / 2;
374 int local_x_offset = 0;
375 if (horizontally_center) local_x_offset = ((x_size * nedsim->cell_size) - text_x_size) / 2;
376
b1906d46
AT
377 XDrawString(nedsim->dpy, nedsim->panel, nedsim->gc,
378 (x_origin * nedsim->cell_size + nedsim->origin_x_offset + local_x_offset),
379 ((y_origin + y_size) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset),
380 text, strlen(text)
381 );
382}
383
384/* -------------------------------------------------------------------------- */
385/* Dynamic Panel Functions */
386/* -------------------------------------------------------------------------- */
387
388// Draw the 32 lights corresponding to 'word' at coordinates ('x','y').
389// Note that this function ONLY draws the lights and is used each frame for
390// updating the panel. To draw the wordline area itself, use draw_wordline().
391static void
392draw_wordline_lights(struct NEDsim * nedsim, uint32_t word, int x, int y)
393{
394 for (int i = 0; i < WORDLINE_WIDTH; i++) {
395 if (word & (1<<(WORDLINE_WIDTH-1-i))) {
396 set_color(nedsim, COLOR(light_on));
397 } else {
398 set_color(nedsim, COLOR(light_off));
399 }
400 draw_circular_area(nedsim, x+i, y, WORDLINE_HEIGHT);
401 }
402}
403
404// After the static front panel has been drawn at least once, this function
405// updates all the dynamic parts of the panel (lights + text values) and is
406// called every frame.
407static void
408update_display(struct NEDsim * nedsim)
409{
410 // Draw the halt indicator.
411 if (nedsim->nedstate->halted) {
412 set_color(nedsim, COLOR(error_on));
413 } else {
414 set_color(nedsim, COLOR(error_off));
415 }
416 draw_circular_area(nedsim, HALT_X_OFFSET, HALT_Y_OFFSET, HALT_WIDTH);
417
418 // Draw the PSW "N" light.
419 if (nedsim->nedstate->active_thread->psw->negative) {
420 set_color(nedsim, COLOR(light_on));
421 } else {
422 set_color(nedsim, COLOR(light_off));
423 }
424 draw_circular_area(nedsim,
425 PSW_N_X_OFFSET+1, PSW_Y_OFFSET+PSW_LABEL_HEIGHT, PSW_LIGHT_HEIGHT
426 );
427
428 // Draw the PSW "Z" light.
429 if (nedsim->nedstate->active_thread->psw->zero) {
430 set_color(nedsim, COLOR(light_on));
431 } else {
432 set_color(nedsim, COLOR(light_off));
433 }
434 draw_circular_area(nedsim,
435 PSW_Z_X_OFFSET+1, PSW_Y_OFFSET+PSW_LABEL_HEIGHT, PSW_LIGHT_HEIGHT
436 );
437
438
439 // Draw the SC.
440 for (int i = 0; i < SC_WIDTH; i++) {
441 if ((SC_WIDTH-1-i) == nedsim->nedstate->active_thread->sc) {
442 set_color(nedsim, COLOR(light_on));
443 } else {
444 set_color(nedsim, COLOR(light_off));
445 }
446 draw_circular_area(nedsim,
447 SC_X_OFFSET+i, SC_Y_OFFSET+SC_LABEL_HEIGHT, SC_LIGHT_HEIGHT
448 );
449 }
450
451 // Draw the PC.
452 draw_wordline_lights(nedsim, nedsim->nedstate->active_thread->pc,
453 PC_X_OFFSET, PC_Y_OFFSET+PC_LABEL_HEIGHT
454 );
455
456 // Draw the stack lights.
457 int64_t top_of_stack = ((int64_t)nedsim->nedstate->active_thread->sp) - 1;
458 for (int i = 0; i < nedsim->num_data_rows; i++) {
459 if ((top_of_stack-i) >= 0) {
460 draw_wordline_lights(nedsim,
461 nedsim->nedstate->active_thread->stack[top_of_stack-i],
462 STACK_X_OFFSET, STACK_Y_OFFSET+STACK_LABEL_HEIGHT+i
463 );
464 } else {
465 draw_wordline_lights(nedsim, 0,
466 STACK_X_OFFSET, STACK_Y_OFFSET+STACK_LABEL_HEIGHT+i
467 );
468 }
469 }
470
471 // Draw the stack size in text.
472 draw_rect_area(nedsim, COLOR(tertiary),
473 STACK_X_OFFSET+(STACK_WIDTH/2), STACK_Y_OFFSET,
474 STACK_WIDTH/2, STACK_LABEL_HEIGHT,
475 True, True, False, False
476 );
477 char stack_size[11];
478 snprintf(stack_size, sizeof(stack_size),"0x%08X", nedsim->nedstate->active_thread->sp);
479 draw_text(nedsim, stack_size,
480 (STACK_X_OFFSET+(STACK_WIDTH/2)+1), STACK_Y_OFFSET,
481 STACK_WIDTH, STACK_LABEL_HEIGHT, False
482 );
483
484 // Draw the heap lights.
485 for (int i = 0; i < nedsim->num_data_rows; i++) {
486 draw_wordline_lights(nedsim,
487 ram_r_word(nedsim->nedstate, HEAP_START_ADDRESS+(i*BPW)),
488 HEAP_X_OFFSET, HEAP_Y_OFFSET+HEAP_LABEL_HEIGHT+i
489 );
490 }
76081fc3
AT
491}
492
b1906d46
AT
493/* -------------------------------------------------------------------------- */
494/* Static Panel Functions */
495/* -------------------------------------------------------------------------- */
496
280460e6
AT
497// Draws the panel itself. Not the lights/labels/etc, but the flat sheet of
498// metal that is the front panel.
84b74595
AT
499static void
500draw_panel(struct NEDsim * nedsim)
501{
b1906d46
AT
502 size_t overall_height_in_cells = HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS;
503
84b74595 504 // Draw background color over entire window.
7361f6db 505 set_color(nedsim, COLOR(panel_bg));
839b757b 506 XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc, 0, 0, nedsim->dpy_width, nedsim->dpy_height);
84b74595
AT
507
508 // Draw NED panel in foreground color.
b1906d46
AT
509 draw_rect_area(nedsim, COLOR(panel_fg),
510 0, 0,
511 OVERALL_WIDTH_IN_CELLS, overall_height_in_cells,
512 False, False, False, False
84b74595 513 );
a98a69c1
AT
514
515 // Give the panel rounded corners by first deleting the four right-angle corners...
b1906d46
AT
516 draw_rect_area(nedsim, COLOR(panel_bg),
517 0, 0,
518 1, 1,
519 False, False, False, False
a98a69c1 520 );
b1906d46
AT
521 draw_rect_area(nedsim, COLOR(panel_bg),
522 OVERALL_WIDTH_IN_CELLS-1, 0,
523 1, 1,
524 False, False, False, False
a98a69c1 525 );
b1906d46
AT
526 draw_rect_area(nedsim, COLOR(panel_bg),
527 0, overall_height_in_cells-1,
528 1, 1,
529 False, False, False, False
a98a69c1 530 );
b1906d46
AT
531 draw_rect_area(nedsim, COLOR(panel_bg),
532 OVERALL_WIDTH_IN_CELLS-1, overall_height_in_cells-1,
533 1, 1,
534 False, False, False, False
a98a69c1
AT
535 );
536 // ...and then replacing them with filled arcs, forming rounded corners.
b1906d46
AT
537 // Note that we use XFillArc() instead of draw_circular_area() since we do
538 // not desire any padding.
7361f6db 539 set_color(nedsim, COLOR(panel_fg));
839b757b 540 XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc,
a98a69c1
AT
541 nedsim->origin_x_offset,
542 nedsim->origin_y_offset,
543 nedsim->cell_size * 2, nedsim->cell_size * 2,
544 180*64, -90*64
545 );
839b757b 546 XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc,
a98a69c1
AT
547 nedsim->origin_x_offset + (nedsim->cell_size * (OVERALL_WIDTH_IN_CELLS-2)),
548 nedsim->origin_y_offset,
549 nedsim->cell_size * 2, nedsim->cell_size * 2,
550 0, 90*64
551 );
839b757b 552 XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc,
a98a69c1 553 nedsim->origin_x_offset,
b1906d46 554 nedsim->origin_y_offset + (nedsim->cell_size * (overall_height_in_cells-2)),
a98a69c1
AT
555 nedsim->cell_size * 2, nedsim->cell_size * 2,
556 180*64, 90*64
557 );
839b757b 558 XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc,
a98a69c1 559 nedsim->origin_x_offset + (nedsim->cell_size * (OVERALL_WIDTH_IN_CELLS-2)),
b1906d46 560 nedsim->origin_y_offset + (nedsim->cell_size * (overall_height_in_cells-2)),
a98a69c1
AT
561 nedsim->cell_size * 2, nedsim->cell_size * 2,
562 0, -90*64
563 );
84b74595
AT
564}
565
84b74595
AT
566static void
567draw_logo(struct NEDsim * nedsim)
568{
b1906d46
AT
569 draw_rect_area(nedsim, COLOR(primary),
570 LOGO_X_OFFSET, LOGO_Y_OFFSET,
571 LOGO_WIDTH, LOGO_NAME_HEIGHT,
572 True, True, False, False
573 );
574 draw_rect_area(nedsim, COLOR(tertiary),
575 LOGO_X_OFFSET, LOGO_Y_OFFSET+LOGO_NAME_HEIGHT,
576 LOGO_WIDTH, LOGO_WEBSITE_HEIGHT,
577 False, True, False, False
578 );
84b74595 579 set_font_size(nedsim, LOGO_NAME_HEIGHT);
b1906d46
AT
580 draw_text(nedsim, "NED", LOGO_X_OFFSET, LOGO_Y_OFFSET,
581 LOGO_WIDTH, LOGO_NAME_HEIGHT, True
582 );
84b74595 583 set_font_size(nedsim, LOGO_WEBSITE_HEIGHT);
b1906d46
AT
584 draw_text(nedsim, "subgeniuskitty.com", LOGO_X_OFFSET, LOGO_Y_OFFSET+LOGO_NAME_HEIGHT,
585 LOGO_WIDTH, LOGO_WEBSITE_HEIGHT, True
586 );
84b74595
AT
587}
588
84b74595
AT
589static void
590draw_halt(struct NEDsim * nedsim)
591{
b1906d46
AT
592 draw_rect_area(nedsim, COLOR(tertiary),
593 HALT_X_OFFSET, HALT_Y_OFFSET,
594 HALT_WIDTH, HALT_LIGHT_HEIGHT,
595 True, True, False, False
596 );
597 draw_rect_area(nedsim, COLOR(secondary),
598 HALT_X_OFFSET, HALT_Y_OFFSET+HALT_LIGHT_HEIGHT,
599 HALT_WIDTH, HALT_LABEL_HEIGHT,
600 False, True, False, False
601 );
602 draw_text(nedsim, "HALT", HALT_X_OFFSET, HALT_Y_OFFSET+HALT_LIGHT_HEIGHT,
603 HALT_WIDTH, HALT_LABEL_HEIGHT, True
604 );
84b74595
AT
605}
606
b1906d46
AT
607// Draw a single 32-bit NED word line at coordinates ('x','y'). To update with
608// a specific value, call draw_wordline_lights() after drawing the wordline
609// area at least once.
84b74595
AT
610static void
611draw_wordline(struct NEDsim * nedsim, int x, int y)
612{
613 // First, draw a solid box in the primary color over the entire wordline area.
b1906d46
AT
614 draw_rect_area(nedsim, COLOR(primary),
615 x, y,
616 WORDLINE_WIDTH, WORDLINE_HEIGHT,
617 False, True, False, False
618 );
84b74595
AT
619
620 // Now, draw stripes in the secondary color.
0eb8595f 621 for (int i = 0; i < (WORDLINE_WIDTH/(2*WORDLINE_BITS_PER_STRIPE)); i++) {
b1906d46
AT
622 draw_rect_area(nedsim, COLOR(secondary),
623 (x+(i*(WORDLINE_WIDTH/WORDLINE_BITS_PER_STRIPE))), y,
624 WORDLINE_BITS_PER_STRIPE, WORDLINE_HEIGHT,
625 False, True, False, False
626 );
84b74595 627 }
84b74595
AT
628}
629
84b74595
AT
630static void
631draw_pc(struct NEDsim * nedsim)
632{
b1906d46
AT
633 draw_rect_area(nedsim, COLOR(tertiary),
634 PC_X_OFFSET, PC_Y_OFFSET,
635 PC_WIDTH, PC_LABEL_HEIGHT,
636 True, True, False, False
637 );
84b74595 638 draw_wordline(nedsim, PC_X_OFFSET, PC_Y_OFFSET+PC_LABEL_HEIGHT);
76081fc3 639 draw_text(nedsim, "PC", PC_X_OFFSET, PC_Y_OFFSET, PC_WIDTH, PC_LABEL_HEIGHT, True);
84b74595
AT
640}
641
84b74595
AT
642static void
643draw_sc(struct NEDsim * nedsim)
644{
b1906d46
AT
645 draw_rect_area(nedsim, COLOR(secondary),
646 SC_X_OFFSET, SC_Y_OFFSET,
647 SC_WIDTH, SC_LABEL_HEIGHT,
648 True, True, False, False
649 );
650 draw_rect_area(nedsim, COLOR(tertiary),
651 SC_X_OFFSET, SC_Y_OFFSET+SC_LABEL_HEIGHT,
652 SC_WIDTH, SC_LIGHT_HEIGHT,
653 False, True, False, False
654 );
76081fc3 655 draw_text(nedsim, "SC", SC_X_OFFSET, SC_Y_OFFSET, SC_WIDTH, SC_LABEL_HEIGHT, True);
84b74595
AT
656}
657
84b74595
AT
658static void
659draw_psw(struct NEDsim * nedsim)
660{
b1906d46
AT
661 draw_rect_area(nedsim, COLOR(secondary),
662 PSW_N_X_OFFSET, PSW_Y_OFFSET,
663 PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT,
664 True, True, False, False
665 );
666 draw_rect_area(nedsim, COLOR(secondary),
667 PSW_Z_X_OFFSET, PSW_Y_OFFSET,
668 PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT,
669 True, True, False, False
670 );
671 draw_rect_area(nedsim, COLOR(tertiary),
672 (PSW_N_X_OFFSET + 1), PSW_Y_OFFSET+PSW_LABEL_HEIGHT,
673 PSW_LIGHT_WIDTH, PSW_LIGHT_HEIGHT,
674 False, True, False, False
675 );
676 draw_rect_area(nedsim, COLOR(tertiary),
677 (PSW_Z_X_OFFSET + 1), PSW_Y_OFFSET+PSW_LABEL_HEIGHT,
678 PSW_LIGHT_WIDTH, PSW_LIGHT_HEIGHT,
679 False, True, False, False
680 );
681 draw_text(nedsim, "N", PSW_N_X_OFFSET, PSW_Y_OFFSET,
682 PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT, True
683 );
684 draw_text(nedsim, "Z", PSW_Z_X_OFFSET, PSW_Y_OFFSET,
685 PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT, True
686 );
84b74595
AT
687}
688
84b74595
AT
689static void
690draw_stack(struct NEDsim * nedsim)
691{
b1906d46
AT
692 draw_rect_area(nedsim, COLOR(tertiary),
693 STACK_X_OFFSET, STACK_Y_OFFSET,
694 STACK_WIDTH, STACK_LABEL_HEIGHT,
695 True, True, False, False
696 );
84b74595
AT
697 for (int i = 0; i < nedsim->num_data_rows; i++) {
698 draw_wordline(nedsim, STACK_X_OFFSET, STACK_Y_OFFSET+STACK_LABEL_HEIGHT+i);
699 }
b1906d46
AT
700 draw_text(nedsim, "Stack Size:", STACK_X_OFFSET+1, STACK_Y_OFFSET,
701 STACK_WIDTH, STACK_LABEL_HEIGHT, False
702 );
84b74595
AT
703}
704
84b74595
AT
705static void
706draw_heap(struct NEDsim * nedsim)
707{
b1906d46
AT
708 draw_rect_area(nedsim, COLOR(tertiary),
709 HEAP_X_OFFSET, HEAP_Y_OFFSET,
710 HEAP_WIDTH, HEAP_LABEL_HEIGHT,
711 True, True, False, False
712 );
84b74595
AT
713 for (int i = 0; i < nedsim->num_data_rows; i++) {
714 draw_wordline(nedsim, HEAP_X_OFFSET, HEAP_Y_OFFSET+HEAP_LABEL_HEIGHT+i);
715 }
b1906d46
AT
716 draw_text(nedsim, "RAM Base:", HEAP_X_OFFSET+1, HEAP_Y_OFFSET,
717 HEAP_WIDTH, HEAP_LABEL_HEIGHT, False
718 );
84b74595
AT
719 char address[11];
720 snprintf(address, sizeof(address), "0x%08X", HEAP_START_ADDRESS);
b1906d46
AT
721 draw_text(nedsim, address, (HEAP_X_OFFSET+(HEAP_WIDTH/2)+1), HEAP_Y_OFFSET,
722 HEAP_WIDTH, HEAP_LABEL_HEIGHT, False
723 );
84b74595
AT
724}
725
726/* -------------------------------------------------------------------------- */
727/* Screenhack API Functions */
728/* -------------------------------------------------------------------------- */
729
730static Bool
731NEDsim_event(Display * dpy, Window win, void * closure, XEvent * event)
732{
733 return False;
734}
735
736static void
737NEDsim_free(Display * dpy, Window win, void * closure)
738{
84b74595 739 struct NEDsim * nedsim = closure;
d87b1e06
AT
740
741 if (nedsim->nedstate != NULL) {
742 free(nedsim->nedstate->active_thread->psw);
743 free(nedsim->nedstate->active_thread);
744 free(nedsim->nedstate->hack);
745 free(nedsim->nedstate);
746 }
747
84b74595 748 XFreeGC(nedsim->dpy, nedsim->gc);
839b757b 749 XFreePixmap(nedsim->dpy, nedsim->panel);
84b74595
AT
750 free(nedsim);
751}
752
753static void *
754NEDsim_init(Display * dpy, Window win)
755{
b1906d46 756 // Basic Setup ---------------------------------------------------------------------------------
2c7168ae 757
84b74595 758 struct NEDsim * nedsim;
84b74595
AT
759
760 nedsim = calloc(1, sizeof(*nedsim));
761 if (!nedsim) {
762 fprintf(stderr, "ERROR: Failed to calloc() for NEDsim struct in NEDsim_init().\n");
763 exit(EXIT_FAILURE);
764 }
765
766 nedsim->dpy = dpy;
767 nedsim->win = win;
768
2c7168ae
AT
769 XGCValues gcv;
770 nedsim->gc = XCreateGC(nedsim->dpy, nedsim->win, GCForeground, &gcv);
771
772 XWindowAttributes xgwa;
84b74595
AT
773 XGetWindowAttributes(nedsim->dpy, nedsim->win, &xgwa);
774 nedsim->dpy_width = xgwa.width;
775 nedsim->dpy_height = xgwa.height;
776
2c7168ae
AT
777 nedsim->panel = XCreatePixmap(nedsim->dpy, nedsim->win, nedsim->dpy_width, nedsim->dpy_height, xgwa.depth);
778
b1906d46 779 // Process User Requests -----------------------------------------------------------------------
2c7168ae 780
84b74595
AT
781 nedsim->delay = get_integer_resource(nedsim->dpy, "delay", "Integer");
782 nedsim->delay *= 1000; /* Turn milliseconds into microseconds. */
783
2c7168ae
AT
784 // If the user did not supply a program for execution, randomly select one
785 // of the included programs.
5923644e
AT
786 char * input_file = get_string_resource(nedsim->dpy, "binary", "String");
787 if (input_file == NULL) {
788 // TODO: Need to include some default programs and randomly select one to load into RAM.
789 nedsim->nedstate = init_simulator("./test.out");
790 } else {
791 nedsim->nedstate = init_simulator(input_file);
792 }
84b74595 793
2c7168ae 794 // If the user did not select a color scheme, select one randomly.
f93fc861
AT
795 nedsim->color_index = get_integer_resource(nedsim->dpy, "color", "Integer");
796 if (nedsim->color_index == -1) {
797 nedsim->color_index = random() % sizeof(color_list)/sizeof(color_list[0]);
798 } else if (nedsim->color_index >= sizeof(color_list)/sizeof(color_list[0])) {
799 fprintf(stderr, "WARNING: Color index out of range.\n");
800 nedsim->color_index = 0;
801 }
84b74595 802
b1906d46 803 // Scale Panel To Screen -----------------------------------------------------------------------
84b74595 804
2c7168ae
AT
805 // We desire to make the panel as wide as possible while also making the
806 // cell size an integer pixel size.
807 nedsim->cell_size = nedsim->dpy_width / OVERALL_WIDTH_IN_CELLS;
808 // Cell sizes below about 10 pixels result in unreadable fonts. For now,
809 // we'll use that as our cutoff. It also works well with a 1:10 border:cell
810 // ratio.
84b74595
AT
811 if (nedsim->cell_size < 10) {
812 nedsim->suitable_display = False;
813 return nedsim;
814 }
2c7168ae
AT
815 // Center the panel horizontally.
816 nedsim->origin_x_offset = (nedsim->dpy_width -
817 (nedsim->cell_size * OVERALL_WIDTH_IN_CELLS)) / 2;
818
819 // The stack and heap displays are variable height. Size them to fit the
820 // display window.
821 int available_space_for_data_rows = nedsim->dpy_height -
822 (nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + FOOTER_HEIGHT_IN_CELLS));
84b74595 823 for (int i = 0; ; i++) {
37f7b288
AT
824 if ((nedsim->cell_size * i) > available_space_for_data_rows) {
825 nedsim->num_data_rows = --i;
84b74595
AT
826 break;
827 }
828 }
2c7168ae 829 // Enforce a minimum vertical size, though '4' is arbitrary.
84b74595
AT
830 if (nedsim->num_data_rows < 4) {
831 nedsim->suitable_display = False;
832 return nedsim;
833 }
2c7168ae
AT
834 // Center the panel vertically.
835 nedsim->origin_y_offset = (nedsim->dpy_height -
836 (nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS))) / 2;
84b74595
AT
837
838 // Scale border relative to cell_size in a 1:10 relationship.
839 nedsim->border_size = nedsim->cell_size / 10;
840
b1906d46 841 // Draw Initial Panel --------------------------------------------------------------------------
2c7168ae 842
84b74595
AT
843 draw_panel(nedsim);
844 draw_logo(nedsim);
845 draw_halt(nedsim);
846 draw_pc(nedsim);
847 draw_sc(nedsim);
848 draw_psw(nedsim);
849 draw_stack(nedsim);
850 draw_heap(nedsim);
851
852 nedsim->suitable_display = True;
853
854 return nedsim;
855}
856
857static unsigned long
858NEDsim_draw(Display * dpy, Window win, void * closure)
859{
860 struct NEDsim * nedsim = closure;
861
2c7168ae 862 // Update the panel display buffer.
84b74595
AT
863 if (nedsim->suitable_display) {
864 nedsim->nedstate = run_simulator(nedsim->nedstate);
865 update_display(nedsim);
866 } else {
7361f6db 867 set_color(nedsim, COLOR(error_on));
839b757b 868 XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc, 0, 0, nedsim->dpy_width, nedsim->dpy_height);
84b74595
AT
869 }
870
2c7168ae 871 // Copy panel buffer to display window.
839b757b
AT
872 XCopyArea(nedsim->dpy, nedsim->panel, nedsim->win, nedsim->gc, 0, 0, nedsim->dpy_width, nedsim->dpy_height, 0, 0);
873
84b74595
AT
874 return nedsim->delay;
875}
876
877static void
878NEDsim_reshape(Display * dpy, Window win, void * closure, unsigned int w, unsigned int h)
879{
880 struct NEDsim * nedsim = closure;
2c7168ae
AT
881
882 // Only re-initialize the display if it changed size.
84b74595
AT
883 XWindowAttributes xgwa;
884 XGetWindowAttributes(nedsim->dpy, nedsim->win, &xgwa);
84b74595 885 if (nedsim->dpy_width != xgwa.width || nedsim->dpy_height != xgwa.height) {
2c7168ae
AT
886 // Although we are re-initializing the display, we wish to retain the
887 // in-progress NED simulation. Thus, we retain the NEDstate pointer.
503470d7
AT
888 struct NEDstate * original_nedstate = nedsim->nedstate;
889 nedsim->nedstate = NULL;
890 NEDsim_free(dpy, win, nedsim);
891 struct NEDsim * new_nedsim = NEDsim_init(dpy, win);
892 new_nedsim->nedstate = original_nedstate;
893 closure = new_nedsim;
84b74595
AT
894 }
895}
896
897static const char * NEDsim_defaults[] = {
898 "*delay: 250",
f93fc861 899 "*color: -1",
84b74595
AT
900 0
901};
902
903static XrmOptionDescRec NEDsim_options[] = {
904 { "-delay", ".delay", XrmoptionSepArg, 0 },
5923644e 905 { "-binary", ".binary", XrmoptionSepArg, 0 },
f93fc861 906 { "-color", ".color", XrmoptionSepArg, 0 },
84b74595
AT
907 { 0, 0, 0, 0 }
908};
909
910XSCREENSAVER_MODULE ("Blinken-lights simulator for NED1 CPU architecture.", NEDsim)