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