Martin Roth | 9b1b335 | 2016-02-24 12:27:06 -0800 | [diff] [blame] | 1 | /* screen_buffer.c - MemTest-86 Version 3.3 |
| 2 | * |
| 3 | * Released under version 2 of the Gnu Public License. |
| 4 | * By Jani Averbach, Jaa@iki.fi, 2001 |
| 5 | */ |
| 6 | |
| 7 | #include "test.h" |
| 8 | #include "screen_buffer.h" |
| 9 | |
| 10 | #define SCREEN_X 80 |
| 11 | #define SCREEN_Y 25 |
| 12 | #define Y_SIZE SCREEN_Y |
| 13 | /* |
| 14 | * X-size should by one of by screen size, |
| 15 | * so that there is room for ending '\0' |
| 16 | */ |
| 17 | #define X_SIZE SCREEN_X+1 |
| 18 | |
| 19 | static char screen_buf[Y_SIZE][X_SIZE]; |
| 20 | |
| 21 | #ifdef SCRN_DEBUG |
| 22 | |
| 23 | char *padding = "12345678901234567890123456789012345678901234567890123456789012345678901234567890"; |
| 24 | |
| 25 | #define CHECK_BOUNDS(y,x) do {if (y < 0 || Y_SIZE <= y || x < 0 || X_SIZE <= x) print_error("out of index");}while(0) |
| 26 | |
| 27 | #else /* ! SCRN_DEBUG */ |
| 28 | |
| 29 | #define CHECK_BOUNDS(y,x) |
| 30 | |
| 31 | #endif /* SCRN_DEBUG */ |
| 32 | |
| 33 | char |
| 34 | get_scrn_buf(const int y, |
| 35 | const int x) |
| 36 | { |
| 37 | CHECK_BOUNDS(y,x); |
| 38 | return screen_buf[y][x]; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | void |
| 43 | set_scrn_buf(const int y, |
| 44 | const int x, |
| 45 | const char val) |
| 46 | { |
| 47 | CHECK_BOUNDS(y,x); |
| 48 | screen_buf[y][x] = val; |
| 49 | } |
| 50 | |
| 51 | void clear_screen_buf() |
| 52 | { |
| 53 | int y, x; |
| 54 | |
| 55 | for (y=0; y < SCREEN_Y; ++y){ |
| 56 | for (x=0; x < SCREEN_X; ++x){ |
| 57 | CHECK_BOUNDS(y,x); |
| 58 | screen_buf[y][x] = ' '; |
| 59 | } |
| 60 | CHECK_BOUNDS(y,SCREEN_X); |
| 61 | screen_buf[y][SCREEN_X] = '\0'; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void tty_print_region(const int pi_top, |
| 66 | const int pi_left, |
| 67 | const int pi_bottom, |
| 68 | const int pi_right) |
| 69 | { |
| 70 | int y; |
| 71 | char tmp; |
| 72 | |
| 73 | for (y=pi_top; y < pi_bottom; ++y){ |
| 74 | CHECK_BOUNDS(y, pi_right); |
| 75 | |
| 76 | tmp = screen_buf[y][pi_right]; |
| 77 | screen_buf[y][pi_right] = '\0'; |
| 78 | |
| 79 | CHECK_BOUNDS(y, pi_left); |
| 80 | ttyprint(y, pi_left, &(screen_buf[y][pi_left])); |
| 81 | |
| 82 | screen_buf[y][pi_right] = tmp; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void tty_print_line( |
| 87 | int y, int x, const char *text) |
| 88 | { |
| 89 | for(; *text && (x < SCREEN_X); x++, text++) { |
| 90 | if (*text != screen_buf[y][x]) { |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | /* If there is nothing to do return */ |
| 95 | if (*text == '\0') { |
| 96 | return; |
| 97 | } |
| 98 | ttyprint(y, x, text); |
| 99 | for(; *text && (x < SCREEN_X); x++, text++) { |
| 100 | screen_buf[y][x] = *text; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | |
| 105 | void tty_print_screen(void) |
| 106 | { |
| 107 | #ifdef SCRN_DEBUG |
| 108 | int i; |
| 109 | |
| 110 | for (i=0; i < SCREEN_Y; ++i) |
| 111 | ttyprint(i,0, padding); |
| 112 | #endif /* SCRN_DEBUG */ |
| 113 | |
| 114 | tty_print_region(0, 0, SCREEN_Y, SCREEN_X); |
| 115 | } |
| 116 | |
| 117 | void print_error(char *pstr) |
| 118 | { |
| 119 | |
| 120 | #ifdef SCRN_DEBUG |
| 121 | ttyprint(0,0, padding); |
| 122 | #endif /* SCRN_DEBUG */ |
| 123 | |
| 124 | ttyprint(0,35, pstr); |
| 125 | |
| 126 | while(1); |
| 127 | } |