Cleaned up all the helper and drawing function areas in NEDsim.
[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
84b74595 212// TODO: Make this a lot faster.
280460e6 213// Set font size to fill 'size' cells vertically, minus space for border and padding.
84b74595
AT
214static void
215set_font_size(struct NEDsim * nedsim, int size)
216{
217 // vvv--- for border ---vvv vvv--- for padding ---vvv
218 int desired_height_in_pixels = (size * nedsim->cell_size) - (2 * nedsim->border_size) - (8 * nedsim->border_size);
219
220 const char * font_size_prefix = "-*-helvetica-*-r-*-*-";
221 const char * font_size_suffix = "-*-*-*-*-*-*-*";
222
223 size_t buffer_size = strlen(font_size_prefix) + strlen(font_size_suffix) + 100; // '100' since nobody needs font with size in 'points' greater than 100 decimal digits long.
224 char * font_full_name = malloc(buffer_size);
225 int font_size_in_points = 2; // Start with a 2 pt font and work our way up.
226
227 while (1) {
228 // Load the font.
229 snprintf(font_full_name, buffer_size, "%s%d%s", font_size_prefix, font_size_in_points, font_size_suffix);
230 XFontStruct * font = XLoadQueryFont(nedsim->dpy, font_full_name);
231 if (!font) {
232 printf("WARNING: Unable to load font %s. Probably gonna look wonky.\n", font_full_name);
233 font = XLoadQueryFont(nedsim->dpy, "fixed");
234 break;
235 }
236 XSetFont(nedsim->dpy, nedsim->gc, font->fid);
237
238 // Get the height.
239 int direction, ascent, descent;
240 XCharStruct overall;
241 XTextExtents(font, "X", 1, &direction, &ascent, &descent, &overall);
242
243 // Compare the height.
244 int height = overall.ascent - overall.descent;
245 if (height == desired_height_in_pixels) {
246 break;
247 } else if (height > desired_height_in_pixels) {
248 font_size_in_points--;
249 snprintf(font_full_name, buffer_size, "%s%d%s", font_size_prefix, font_size_in_points, font_size_suffix);
250 XFontStruct * font = XLoadQueryFont(nedsim->dpy, font_full_name);
251 if (!font) {
252 printf("WARNING: Unable to load font %s. Probably gonna look wonky.\n", font_full_name);
253 font = XLoadQueryFont(nedsim->dpy, "fixed");
254 break;
255 }
256 XSetFont(nedsim->dpy, nedsim->gc, font->fid);
257 break;
258 } else {
259 font_size_in_points++;
260 }
261 }
262
263 free(nedsim->current_font);
264 nedsim->current_font = font_full_name;
265}
266
280460e6
AT
267// Unlike most functions in this program that input/output in units of 'cells',
268// this function uses units of 'pixels' so the caller can track fractional cell
269// usage (e.g. for centering the text).
84b74595
AT
270static void
271get_text_size(struct NEDsim * nedsim, const char * text, int * x_size, int * y_size)
272{
273 int direction, ascent, descent;
274 XCharStruct overall;
275 XFontStruct * font = XLoadQueryFont(nedsim->dpy, nedsim->current_font);
276 XTextExtents(font, text, strlen(text), &direction, &ascent, &descent, &overall);
277 *x_size = overall.width;
278 *y_size = overall.ascent - overall.descent;
279}
280
280460e6 281// When specifying the rectangular area to draw, all coordinates and sizes are
b1906d46 282// in units of 'cells'.
84b74595 283static void
33c35ba0
AT
284draw_rect_area(struct NEDsim * nedsim, struct color_rgb * color,
285 size_t x_origin, size_t y_origin, size_t x_size, size_t y_size,
84b74595
AT
286 Bool bord_top, Bool bord_bottom, Bool bord_left, Bool bord_right)
287{
280460e6 288 // First fill in the rectangular area...
84b74595
AT
289 x_origin *= nedsim->cell_size;
290 x_origin += nedsim->origin_x_offset;
291 y_origin *= nedsim->cell_size;
292 y_origin += nedsim->origin_y_offset;
293 x_size *= nedsim->cell_size;
294 y_size *= nedsim->cell_size;
33c35ba0 295 set_color(nedsim, color);
b1906d46
AT
296 XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
297 x_origin, y_origin, x_size, y_size
298 );
84b74595 299
280460e6 300 // ...then give it a border, if requested.
7361f6db 301 set_color(nedsim, COLOR(border));
84b74595 302 if (bord_top) {
280460e6 303 XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
b1906d46
AT
304 x_origin, y_origin,
305 x_size, nedsim->border_size
306 );
84b74595
AT
307 }
308 if (bord_bottom) {
280460e6 309 XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
b1906d46
AT
310 x_origin, (y_origin + y_size - nedsim->border_size),
311 x_size, nedsim->border_size
312 );
84b74595
AT
313 }
314 if (bord_left) {
280460e6 315 XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
b1906d46
AT
316 x_origin, y_origin,
317 nedsim->border_size, y_size
318 );
84b74595
AT
319 }
320 if (bord_right) {
280460e6 321 XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
b1906d46
AT
322 (x_origin + x_size - nedsim->border_size), y_origin,
323 nedsim->border_size, y_size
324 );
84b74595
AT
325 }
326}
327
280460e6
AT
328// Draws filled circle in a square area with upper left corner at x,y. When
329// specifying the location and size to draw, all values are in units of
b1906d46 330// 'cells'.
84b74595
AT
331static void
332draw_circular_area(struct NEDsim * nedsim, size_t x, size_t y, double diameter)
333{
334 // First, convert the function argument units from 'cells' to 'pixels'
335 x *= nedsim->cell_size;
336 y *= nedsim->cell_size;
337 diameter *= nedsim->cell_size;
338
339 // Add the panel's absolute x,y offset to the requested coordinates.
340 x += nedsim->origin_x_offset;
341 y += nedsim->origin_y_offset;
342
343 // Shrink the circle to be a bit smaller than the bounding box allows. Note
344 // that the three adjustment values must sum to 1.0.
345 // For example, 0.7 + 0.15 + 0.15 = 1.0.
346 x += (0.15 * diameter);
347 y += (0.15 * diameter);
348 diameter *= 0.7;
349
350 // Because we only draw the bottom border on repeated rows (e.g. draw_wordline()),
351 // we need to offset vertically by half a border height.
352 y -= (0.5 * nedsim->border_size);
353
354 // Start angle 0 and ending angle 360*64 is one full circle in Xlib units.
839b757b 355 XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc, x, y, diameter, diameter, 0, 360*64);
84b74595
AT
356}
357
76081fc3
AT
358// Draws text in a square area with upper left corner at (x_origin,y_origin).
359// Requires that set_font_size() has been run at least once. All values are in
360// units of 'cells'.
361static void
b1906d46
AT
362draw_text(struct NEDsim * nedsim, const char * text,
363 int x_origin, int y_origin, int x_size, int y_size, Bool horizontally_center)
76081fc3 364{
7361f6db 365 set_color(nedsim, COLOR(text));
76081fc3
AT
366
367 int text_x_size, text_y_size;
368 get_text_size(nedsim, text, &text_x_size, &text_y_size);
369
370 int local_y_offset = ((y_size * nedsim->cell_size) - text_y_size) / 2;
371 int local_x_offset = 0;
372 if (horizontally_center) local_x_offset = ((x_size * nedsim->cell_size) - text_x_size) / 2;
373
b1906d46
AT
374 XDrawString(nedsim->dpy, nedsim->panel, nedsim->gc,
375 (x_origin * nedsim->cell_size + nedsim->origin_x_offset + local_x_offset),
376 ((y_origin + y_size) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset),
377 text, strlen(text)
378 );
379}
380
381/* -------------------------------------------------------------------------- */
382/* Dynamic Panel Functions */
383/* -------------------------------------------------------------------------- */
384
385// Draw the 32 lights corresponding to 'word' at coordinates ('x','y').
386// Note that this function ONLY draws the lights and is used each frame for
387// updating the panel. To draw the wordline area itself, use draw_wordline().
388static void
389draw_wordline_lights(struct NEDsim * nedsim, uint32_t word, int x, int y)
390{
391 for (int i = 0; i < WORDLINE_WIDTH; i++) {
392 if (word & (1<<(WORDLINE_WIDTH-1-i))) {
393 set_color(nedsim, COLOR(light_on));
394 } else {
395 set_color(nedsim, COLOR(light_off));
396 }
397 draw_circular_area(nedsim, x+i, y, WORDLINE_HEIGHT);
398 }
399}
400
401// After the static front panel has been drawn at least once, this function
402// updates all the dynamic parts of the panel (lights + text values) and is
403// called every frame.
404static void
405update_display(struct NEDsim * nedsim)
406{
407 // Draw the halt indicator.
408 if (nedsim->nedstate->halted) {
409 set_color(nedsim, COLOR(error_on));
410 } else {
411 set_color(nedsim, COLOR(error_off));
412 }
413 draw_circular_area(nedsim, HALT_X_OFFSET, HALT_Y_OFFSET, HALT_WIDTH);
414
415 // Draw the PSW "N" light.
416 if (nedsim->nedstate->active_thread->psw->negative) {
417 set_color(nedsim, COLOR(light_on));
418 } else {
419 set_color(nedsim, COLOR(light_off));
420 }
421 draw_circular_area(nedsim,
422 PSW_N_X_OFFSET+1, PSW_Y_OFFSET+PSW_LABEL_HEIGHT, PSW_LIGHT_HEIGHT
423 );
424
425 // Draw the PSW "Z" light.
426 if (nedsim->nedstate->active_thread->psw->zero) {
427 set_color(nedsim, COLOR(light_on));
428 } else {
429 set_color(nedsim, COLOR(light_off));
430 }
431 draw_circular_area(nedsim,
432 PSW_Z_X_OFFSET+1, PSW_Y_OFFSET+PSW_LABEL_HEIGHT, PSW_LIGHT_HEIGHT
433 );
434
435
436 // Draw the SC.
437 for (int i = 0; i < SC_WIDTH; i++) {
438 if ((SC_WIDTH-1-i) == nedsim->nedstate->active_thread->sc) {
439 set_color(nedsim, COLOR(light_on));
440 } else {
441 set_color(nedsim, COLOR(light_off));
442 }
443 draw_circular_area(nedsim,
444 SC_X_OFFSET+i, SC_Y_OFFSET+SC_LABEL_HEIGHT, SC_LIGHT_HEIGHT
445 );
446 }
447
448 // Draw the PC.
449 draw_wordline_lights(nedsim, nedsim->nedstate->active_thread->pc,
450 PC_X_OFFSET, PC_Y_OFFSET+PC_LABEL_HEIGHT
451 );
452
453 // Draw the stack lights.
454 int64_t top_of_stack = ((int64_t)nedsim->nedstate->active_thread->sp) - 1;
455 for (int i = 0; i < nedsim->num_data_rows; i++) {
456 if ((top_of_stack-i) >= 0) {
457 draw_wordline_lights(nedsim,
458 nedsim->nedstate->active_thread->stack[top_of_stack-i],
459 STACK_X_OFFSET, STACK_Y_OFFSET+STACK_LABEL_HEIGHT+i
460 );
461 } else {
462 draw_wordline_lights(nedsim, 0,
463 STACK_X_OFFSET, STACK_Y_OFFSET+STACK_LABEL_HEIGHT+i
464 );
465 }
466 }
467
468 // Draw the stack size in text.
469 draw_rect_area(nedsim, COLOR(tertiary),
470 STACK_X_OFFSET+(STACK_WIDTH/2), STACK_Y_OFFSET,
471 STACK_WIDTH/2, STACK_LABEL_HEIGHT,
472 True, True, False, False
473 );
474 char stack_size[11];
475 snprintf(stack_size, sizeof(stack_size),"0x%08X", nedsim->nedstate->active_thread->sp);
476 draw_text(nedsim, stack_size,
477 (STACK_X_OFFSET+(STACK_WIDTH/2)+1), STACK_Y_OFFSET,
478 STACK_WIDTH, STACK_LABEL_HEIGHT, False
479 );
480
481 // Draw the heap lights.
482 for (int i = 0; i < nedsim->num_data_rows; i++) {
483 draw_wordline_lights(nedsim,
484 ram_r_word(nedsim->nedstate, HEAP_START_ADDRESS+(i*BPW)),
485 HEAP_X_OFFSET, HEAP_Y_OFFSET+HEAP_LABEL_HEIGHT+i
486 );
487 }
76081fc3
AT
488}
489
b1906d46
AT
490/* -------------------------------------------------------------------------- */
491/* Static Panel Functions */
492/* -------------------------------------------------------------------------- */
493
280460e6
AT
494// Draws the panel itself. Not the lights/labels/etc, but the flat sheet of
495// metal that is the front panel.
84b74595
AT
496static void
497draw_panel(struct NEDsim * nedsim)
498{
b1906d46
AT
499 size_t overall_height_in_cells = HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS;
500
84b74595 501 // Draw background color over entire window.
7361f6db 502 set_color(nedsim, COLOR(panel_bg));
839b757b 503 XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc, 0, 0, nedsim->dpy_width, nedsim->dpy_height);
84b74595
AT
504
505 // Draw NED panel in foreground color.
b1906d46
AT
506 draw_rect_area(nedsim, COLOR(panel_fg),
507 0, 0,
508 OVERALL_WIDTH_IN_CELLS, overall_height_in_cells,
509 False, False, False, False
84b74595 510 );
a98a69c1
AT
511
512 // Give the panel rounded corners by first deleting the four right-angle corners...
b1906d46
AT
513 draw_rect_area(nedsim, COLOR(panel_bg),
514 0, 0,
515 1, 1,
516 False, False, False, False
a98a69c1 517 );
b1906d46
AT
518 draw_rect_area(nedsim, COLOR(panel_bg),
519 OVERALL_WIDTH_IN_CELLS-1, 0,
520 1, 1,
521 False, False, False, False
a98a69c1 522 );
b1906d46
AT
523 draw_rect_area(nedsim, COLOR(panel_bg),
524 0, overall_height_in_cells-1,
525 1, 1,
526 False, False, False, False
a98a69c1 527 );
b1906d46
AT
528 draw_rect_area(nedsim, COLOR(panel_bg),
529 OVERALL_WIDTH_IN_CELLS-1, overall_height_in_cells-1,
530 1, 1,
531 False, False, False, False
a98a69c1
AT
532 );
533 // ...and then replacing them with filled arcs, forming rounded corners.
b1906d46
AT
534 // Note that we use XFillArc() instead of draw_circular_area() since we do
535 // not desire any padding.
7361f6db 536 set_color(nedsim, COLOR(panel_fg));
839b757b 537 XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc,
a98a69c1
AT
538 nedsim->origin_x_offset,
539 nedsim->origin_y_offset,
540 nedsim->cell_size * 2, nedsim->cell_size * 2,
541 180*64, -90*64
542 );
839b757b 543 XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc,
a98a69c1
AT
544 nedsim->origin_x_offset + (nedsim->cell_size * (OVERALL_WIDTH_IN_CELLS-2)),
545 nedsim->origin_y_offset,
546 nedsim->cell_size * 2, nedsim->cell_size * 2,
547 0, 90*64
548 );
839b757b 549 XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc,
a98a69c1 550 nedsim->origin_x_offset,
b1906d46 551 nedsim->origin_y_offset + (nedsim->cell_size * (overall_height_in_cells-2)),
a98a69c1
AT
552 nedsim->cell_size * 2, nedsim->cell_size * 2,
553 180*64, 90*64
554 );
839b757b 555 XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc,
a98a69c1 556 nedsim->origin_x_offset + (nedsim->cell_size * (OVERALL_WIDTH_IN_CELLS-2)),
b1906d46 557 nedsim->origin_y_offset + (nedsim->cell_size * (overall_height_in_cells-2)),
a98a69c1
AT
558 nedsim->cell_size * 2, nedsim->cell_size * 2,
559 0, -90*64
560 );
84b74595
AT
561}
562
84b74595
AT
563static void
564draw_logo(struct NEDsim * nedsim)
565{
b1906d46
AT
566 draw_rect_area(nedsim, COLOR(primary),
567 LOGO_X_OFFSET, LOGO_Y_OFFSET,
568 LOGO_WIDTH, LOGO_NAME_HEIGHT,
569 True, True, False, False
570 );
571 draw_rect_area(nedsim, COLOR(tertiary),
572 LOGO_X_OFFSET, LOGO_Y_OFFSET+LOGO_NAME_HEIGHT,
573 LOGO_WIDTH, LOGO_WEBSITE_HEIGHT,
574 False, True, False, False
575 );
84b74595 576 set_font_size(nedsim, LOGO_NAME_HEIGHT);
b1906d46
AT
577 draw_text(nedsim, "NED", LOGO_X_OFFSET, LOGO_Y_OFFSET,
578 LOGO_WIDTH, LOGO_NAME_HEIGHT, True
579 );
84b74595 580 set_font_size(nedsim, LOGO_WEBSITE_HEIGHT);
b1906d46
AT
581 draw_text(nedsim, "subgeniuskitty.com", LOGO_X_OFFSET, LOGO_Y_OFFSET+LOGO_NAME_HEIGHT,
582 LOGO_WIDTH, LOGO_WEBSITE_HEIGHT, True
583 );
84b74595
AT
584}
585
84b74595
AT
586static void
587draw_halt(struct NEDsim * nedsim)
588{
b1906d46
AT
589 draw_rect_area(nedsim, COLOR(tertiary),
590 HALT_X_OFFSET, HALT_Y_OFFSET,
591 HALT_WIDTH, HALT_LIGHT_HEIGHT,
592 True, True, False, False
593 );
594 draw_rect_area(nedsim, COLOR(secondary),
595 HALT_X_OFFSET, HALT_Y_OFFSET+HALT_LIGHT_HEIGHT,
596 HALT_WIDTH, HALT_LABEL_HEIGHT,
597 False, True, False, False
598 );
599 draw_text(nedsim, "HALT", HALT_X_OFFSET, HALT_Y_OFFSET+HALT_LIGHT_HEIGHT,
600 HALT_WIDTH, HALT_LABEL_HEIGHT, True
601 );
84b74595
AT
602}
603
b1906d46
AT
604// Draw a single 32-bit NED word line at coordinates ('x','y'). To update with
605// a specific value, call draw_wordline_lights() after drawing the wordline
606// area at least once.
84b74595
AT
607static void
608draw_wordline(struct NEDsim * nedsim, int x, int y)
609{
610 // First, draw a solid box in the primary color over the entire wordline area.
b1906d46
AT
611 draw_rect_area(nedsim, COLOR(primary),
612 x, y,
613 WORDLINE_WIDTH, WORDLINE_HEIGHT,
614 False, True, False, False
615 );
84b74595
AT
616
617 // Now, draw stripes in the secondary color.
0eb8595f 618 for (int i = 0; i < (WORDLINE_WIDTH/(2*WORDLINE_BITS_PER_STRIPE)); i++) {
b1906d46
AT
619 draw_rect_area(nedsim, COLOR(secondary),
620 (x+(i*(WORDLINE_WIDTH/WORDLINE_BITS_PER_STRIPE))), y,
621 WORDLINE_BITS_PER_STRIPE, WORDLINE_HEIGHT,
622 False, True, False, False
623 );
84b74595 624 }
84b74595
AT
625}
626
84b74595
AT
627static void
628draw_pc(struct NEDsim * nedsim)
629{
b1906d46
AT
630 draw_rect_area(nedsim, COLOR(tertiary),
631 PC_X_OFFSET, PC_Y_OFFSET,
632 PC_WIDTH, PC_LABEL_HEIGHT,
633 True, True, False, False
634 );
84b74595 635 draw_wordline(nedsim, PC_X_OFFSET, PC_Y_OFFSET+PC_LABEL_HEIGHT);
76081fc3 636 draw_text(nedsim, "PC", PC_X_OFFSET, PC_Y_OFFSET, PC_WIDTH, PC_LABEL_HEIGHT, True);
84b74595
AT
637}
638
84b74595
AT
639static void
640draw_sc(struct NEDsim * nedsim)
641{
b1906d46
AT
642 draw_rect_area(nedsim, COLOR(secondary),
643 SC_X_OFFSET, SC_Y_OFFSET,
644 SC_WIDTH, SC_LABEL_HEIGHT,
645 True, True, False, False
646 );
647 draw_rect_area(nedsim, COLOR(tertiary),
648 SC_X_OFFSET, SC_Y_OFFSET+SC_LABEL_HEIGHT,
649 SC_WIDTH, SC_LIGHT_HEIGHT,
650 False, True, False, False
651 );
76081fc3 652 draw_text(nedsim, "SC", SC_X_OFFSET, SC_Y_OFFSET, SC_WIDTH, SC_LABEL_HEIGHT, True);
84b74595
AT
653}
654
84b74595
AT
655static void
656draw_psw(struct NEDsim * nedsim)
657{
b1906d46
AT
658 draw_rect_area(nedsim, COLOR(secondary),
659 PSW_N_X_OFFSET, PSW_Y_OFFSET,
660 PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT,
661 True, True, False, False
662 );
663 draw_rect_area(nedsim, COLOR(secondary),
664 PSW_Z_X_OFFSET, PSW_Y_OFFSET,
665 PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT,
666 True, True, False, False
667 );
668 draw_rect_area(nedsim, COLOR(tertiary),
669 (PSW_N_X_OFFSET + 1), PSW_Y_OFFSET+PSW_LABEL_HEIGHT,
670 PSW_LIGHT_WIDTH, PSW_LIGHT_HEIGHT,
671 False, True, False, False
672 );
673 draw_rect_area(nedsim, COLOR(tertiary),
674 (PSW_Z_X_OFFSET + 1), PSW_Y_OFFSET+PSW_LABEL_HEIGHT,
675 PSW_LIGHT_WIDTH, PSW_LIGHT_HEIGHT,
676 False, True, False, False
677 );
678 draw_text(nedsim, "N", PSW_N_X_OFFSET, PSW_Y_OFFSET,
679 PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT, True
680 );
681 draw_text(nedsim, "Z", PSW_Z_X_OFFSET, PSW_Y_OFFSET,
682 PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT, True
683 );
84b74595
AT
684}
685
84b74595
AT
686static void
687draw_stack(struct NEDsim * nedsim)
688{
b1906d46
AT
689 draw_rect_area(nedsim, COLOR(tertiary),
690 STACK_X_OFFSET, STACK_Y_OFFSET,
691 STACK_WIDTH, STACK_LABEL_HEIGHT,
692 True, True, False, False
693 );
84b74595
AT
694 for (int i = 0; i < nedsim->num_data_rows; i++) {
695 draw_wordline(nedsim, STACK_X_OFFSET, STACK_Y_OFFSET+STACK_LABEL_HEIGHT+i);
696 }
b1906d46
AT
697 draw_text(nedsim, "Stack Size:", STACK_X_OFFSET+1, STACK_Y_OFFSET,
698 STACK_WIDTH, STACK_LABEL_HEIGHT, False
699 );
84b74595
AT
700}
701
84b74595
AT
702static void
703draw_heap(struct NEDsim * nedsim)
704{
b1906d46
AT
705 draw_rect_area(nedsim, COLOR(tertiary),
706 HEAP_X_OFFSET, HEAP_Y_OFFSET,
707 HEAP_WIDTH, HEAP_LABEL_HEIGHT,
708 True, True, False, False
709 );
84b74595
AT
710 for (int i = 0; i < nedsim->num_data_rows; i++) {
711 draw_wordline(nedsim, HEAP_X_OFFSET, HEAP_Y_OFFSET+HEAP_LABEL_HEIGHT+i);
712 }
b1906d46
AT
713 draw_text(nedsim, "RAM Base:", HEAP_X_OFFSET+1, HEAP_Y_OFFSET,
714 HEAP_WIDTH, HEAP_LABEL_HEIGHT, False
715 );
84b74595
AT
716 char address[11];
717 snprintf(address, sizeof(address), "0x%08X", HEAP_START_ADDRESS);
b1906d46
AT
718 draw_text(nedsim, address, (HEAP_X_OFFSET+(HEAP_WIDTH/2)+1), HEAP_Y_OFFSET,
719 HEAP_WIDTH, HEAP_LABEL_HEIGHT, False
720 );
84b74595
AT
721}
722
723/* -------------------------------------------------------------------------- */
724/* Screenhack API Functions */
725/* -------------------------------------------------------------------------- */
726
727static Bool
728NEDsim_event(Display * dpy, Window win, void * closure, XEvent * event)
729{
730 return False;
731}
732
733static void
734NEDsim_free(Display * dpy, Window win, void * closure)
735{
84b74595 736 struct NEDsim * nedsim = closure;
d87b1e06
AT
737
738 if (nedsim->nedstate != NULL) {
739 free(nedsim->nedstate->active_thread->psw);
740 free(nedsim->nedstate->active_thread);
741 free(nedsim->nedstate->hack);
742 free(nedsim->nedstate);
743 }
744
745 // TODO: Replace all this with proper code to free everything related to the screensaver itself.
84b74595 746 XFreeGC(nedsim->dpy, nedsim->gc);
839b757b 747 XFreePixmap(nedsim->dpy, nedsim->panel);
84b74595
AT
748 free(nedsim);
749}
750
751static void *
752NEDsim_init(Display * dpy, Window win)
753{
b1906d46 754 // Basic Setup ---------------------------------------------------------------------------------
2c7168ae 755
84b74595 756 struct NEDsim * nedsim;
84b74595
AT
757
758 nedsim = calloc(1, sizeof(*nedsim));
759 if (!nedsim) {
760 fprintf(stderr, "ERROR: Failed to calloc() for NEDsim struct in NEDsim_init().\n");
761 exit(EXIT_FAILURE);
762 }
763
764 nedsim->dpy = dpy;
765 nedsim->win = win;
766
2c7168ae
AT
767 XGCValues gcv;
768 nedsim->gc = XCreateGC(nedsim->dpy, nedsim->win, GCForeground, &gcv);
769
770 XWindowAttributes xgwa;
84b74595
AT
771 XGetWindowAttributes(nedsim->dpy, nedsim->win, &xgwa);
772 nedsim->dpy_width = xgwa.width;
773 nedsim->dpy_height = xgwa.height;
774
2c7168ae
AT
775 nedsim->panel = XCreatePixmap(nedsim->dpy, nedsim->win, nedsim->dpy_width, nedsim->dpy_height, xgwa.depth);
776
b1906d46 777 // Process User Requests -----------------------------------------------------------------------
2c7168ae 778
84b74595
AT
779 nedsim->delay = get_integer_resource(nedsim->dpy, "delay", "Integer");
780 nedsim->delay *= 1000; /* Turn milliseconds into microseconds. */
781
2c7168ae
AT
782 // If the user did not supply a program for execution, randomly select one
783 // of the included programs.
5923644e
AT
784 char * input_file = get_string_resource(nedsim->dpy, "binary", "String");
785 if (input_file == NULL) {
786 // TODO: Need to include some default programs and randomly select one to load into RAM.
787 nedsim->nedstate = init_simulator("./test.out");
788 } else {
789 nedsim->nedstate = init_simulator(input_file);
790 }
84b74595 791
2c7168ae 792 // If the user did not select a color scheme, select one randomly.
f93fc861
AT
793 nedsim->color_index = get_integer_resource(nedsim->dpy, "color", "Integer");
794 if (nedsim->color_index == -1) {
795 nedsim->color_index = random() % sizeof(color_list)/sizeof(color_list[0]);
796 } else if (nedsim->color_index >= sizeof(color_list)/sizeof(color_list[0])) {
797 fprintf(stderr, "WARNING: Color index out of range.\n");
798 nedsim->color_index = 0;
799 }
84b74595 800
b1906d46 801 // Scale Panel To Screen -----------------------------------------------------------------------
84b74595 802
2c7168ae
AT
803 // We desire to make the panel as wide as possible while also making the
804 // cell size an integer pixel size.
805 nedsim->cell_size = nedsim->dpy_width / OVERALL_WIDTH_IN_CELLS;
806 // Cell sizes below about 10 pixels result in unreadable fonts. For now,
807 // we'll use that as our cutoff. It also works well with a 1:10 border:cell
808 // ratio.
84b74595
AT
809 if (nedsim->cell_size < 10) {
810 nedsim->suitable_display = False;
811 return nedsim;
812 }
2c7168ae
AT
813 // Center the panel horizontally.
814 nedsim->origin_x_offset = (nedsim->dpy_width -
815 (nedsim->cell_size * OVERALL_WIDTH_IN_CELLS)) / 2;
816
817 // The stack and heap displays are variable height. Size them to fit the
818 // display window.
819 int available_space_for_data_rows = nedsim->dpy_height -
820 (nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + FOOTER_HEIGHT_IN_CELLS));
84b74595 821 for (int i = 0; ; i++) {
37f7b288
AT
822 if ((nedsim->cell_size * i) > available_space_for_data_rows) {
823 nedsim->num_data_rows = --i;
84b74595
AT
824 break;
825 }
826 }
2c7168ae 827 // Enforce a minimum vertical size, though '4' is arbitrary.
84b74595
AT
828 if (nedsim->num_data_rows < 4) {
829 nedsim->suitable_display = False;
830 return nedsim;
831 }
2c7168ae
AT
832 // Center the panel vertically.
833 nedsim->origin_y_offset = (nedsim->dpy_height -
834 (nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS))) / 2;
84b74595
AT
835
836 // Scale border relative to cell_size in a 1:10 relationship.
837 nedsim->border_size = nedsim->cell_size / 10;
838
b1906d46 839 // Draw Initial Panel --------------------------------------------------------------------------
2c7168ae 840
84b74595
AT
841 draw_panel(nedsim);
842 draw_logo(nedsim);
843 draw_halt(nedsim);
844 draw_pc(nedsim);
845 draw_sc(nedsim);
846 draw_psw(nedsim);
847 draw_stack(nedsim);
848 draw_heap(nedsim);
849
850 nedsim->suitable_display = True;
851
852 return nedsim;
853}
854
855static unsigned long
856NEDsim_draw(Display * dpy, Window win, void * closure)
857{
858 struct NEDsim * nedsim = closure;
859
2c7168ae 860 // Update the panel display buffer.
84b74595
AT
861 if (nedsim->suitable_display) {
862 nedsim->nedstate = run_simulator(nedsim->nedstate);
863 update_display(nedsim);
864 } else {
7361f6db 865 set_color(nedsim, COLOR(error_on));
839b757b 866 XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc, 0, 0, nedsim->dpy_width, nedsim->dpy_height);
84b74595
AT
867 }
868
2c7168ae 869 // Copy panel buffer to display window.
839b757b
AT
870 XCopyArea(nedsim->dpy, nedsim->panel, nedsim->win, nedsim->gc, 0, 0, nedsim->dpy_width, nedsim->dpy_height, 0, 0);
871
84b74595
AT
872 return nedsim->delay;
873}
874
875static void
876NEDsim_reshape(Display * dpy, Window win, void * closure, unsigned int w, unsigned int h)
877{
878 struct NEDsim * nedsim = closure;
2c7168ae
AT
879
880 // Only re-initialize the display if it changed size.
84b74595
AT
881 XWindowAttributes xgwa;
882 XGetWindowAttributes(nedsim->dpy, nedsim->win, &xgwa);
84b74595 883 if (nedsim->dpy_width != xgwa.width || nedsim->dpy_height != xgwa.height) {
2c7168ae
AT
884 // Although we are re-initializing the display, we wish to retain the
885 // in-progress NED simulation. Thus, we retain the NEDstate pointer.
503470d7
AT
886 struct NEDstate * original_nedstate = nedsim->nedstate;
887 nedsim->nedstate = NULL;
888 NEDsim_free(dpy, win, nedsim);
889 struct NEDsim * new_nedsim = NEDsim_init(dpy, win);
890 new_nedsim->nedstate = original_nedstate;
891 closure = new_nedsim;
84b74595
AT
892 }
893}
894
895static const char * NEDsim_defaults[] = {
896 "*delay: 250",
f93fc861 897 "*color: -1",
84b74595
AT
898 0
899};
900
901static XrmOptionDescRec NEDsim_options[] = {
902 { "-delay", ".delay", XrmoptionSepArg, 0 },
5923644e 903 { "-binary", ".binary", XrmoptionSepArg, 0 },
f93fc861 904 { "-color", ".color", XrmoptionSepArg, 0 },
84b74595
AT
905 { 0, 0, 0, 0 }
906};
907
908XSCREENSAVER_MODULE ("Blinken-lights simulator for NED1 CPU architecture.", NEDsim)