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