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