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