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