|
| 1 | +// include Vircon libraries |
| 2 | +#include "audio.h" |
| 3 | +#include "video.h" |
| 4 | +#include "time.h" |
| 5 | +#include "string.h" |
| 6 | +#include "misc.h" |
| 7 | + |
| 8 | +// include project libraries |
| 9 | +#include "ErrorInfo.h" |
| 10 | + |
| 11 | + |
| 12 | +// --------------------------------------------------------- |
| 13 | +// GENERAL DEFINITIONS |
| 14 | +// --------------------------------------------------------- |
| 15 | + |
| 16 | + |
| 17 | +// BIOS-required regions; these |
| 18 | +// may safely be used by programs |
| 19 | +#define first_region_font 0 |
| 20 | +#define region_white_pixel 256 |
| 21 | + |
| 22 | +// other non-required regions, used |
| 23 | +// to draw the logo and error screens |
| 24 | +#define region_console 300 |
| 25 | +#define region_cartridge 301 |
| 26 | +#define region_down_arrow 302 |
| 27 | +#define region_white_square 303 |
| 28 | + |
| 29 | +// colors for error screens |
| 30 | +#define error_colors_background 0xFF8D4130 |
| 31 | +#define error_colors_title color_yellow |
| 32 | +#define error_colors_description color_white |
| 33 | +#define error_colors_values 0xFF8080FF |
| 34 | + |
| 35 | + |
| 36 | +// --------------------------------------------------------- |
| 37 | +// SUPPORT FUNCTIONS |
| 38 | +// --------------------------------------------------------- |
| 39 | + |
| 40 | + |
| 41 | +void draw_message_screen( error_message* message ) |
| 42 | +{ |
| 43 | + clear_screen( error_colors_background ); |
| 44 | + set_multiply_color( color_white ); |
| 45 | + select_texture( -1 ); |
| 46 | + |
| 47 | + // write title |
| 48 | + set_multiply_color( error_colors_title ); |
| 49 | + print_at( 49, 37, message->title ); |
| 50 | + |
| 51 | + // draw horizontal line |
| 52 | + select_region( region_white_square ); |
| 53 | + set_drawing_scale( 640/2, 1 ); |
| 54 | + draw_region_zoomed_at( 0, 60 ); |
| 55 | + set_drawing_scale( 1, 1 ); |
| 56 | + |
| 57 | + // write description |
| 58 | + set_multiply_color( error_colors_description ); |
| 59 | + print_at( 49, 95, message->description ); |
| 60 | +} |
| 61 | + |
| 62 | +// --------------------------------------------------------- |
| 63 | + |
| 64 | +void print_hex_value( int x, int y, int* name, int value ) |
| 65 | +{ |
| 66 | + // convert the number to hex |
| 67 | + int[ 10 ] hex_string; |
| 68 | + itoa( value, hex_string, 16 ); |
| 69 | + |
| 70 | + // join all text parts |
| 71 | + int[ 60 ] text; |
| 72 | + strcpy( text, name ); |
| 73 | + strcat( text, " = 0x" ); |
| 74 | + strcat( text, hex_string ); |
| 75 | + |
| 76 | + // print the text |
| 77 | + print_at( x, y, text ); |
| 78 | +} |
| 79 | + |
| 80 | +// --------------------------------------------------------- |
| 81 | + |
| 82 | +bool cartridge_connected() |
| 83 | +{ |
| 84 | + asm |
| 85 | + { |
| 86 | + "in R0, CAR_Connected" |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// --------------------------------------------------------- |
| 91 | + |
| 92 | +void request_cartridge() |
| 93 | +{ |
| 94 | + // write a custom message as if it was an error |
| 95 | + error_message no_cartridge_message = |
| 96 | + { |
| 97 | + "NO CARTRIDGE FOUND", |
| 98 | + "To play a game, please power off\n" |
| 99 | + "your console and insert a game\n" |
| 100 | + "cartridge compatible with Vircon32." |
| 101 | + }; |
| 102 | + |
| 103 | + draw_message_screen( &no_cartridge_message ); |
| 104 | + |
| 105 | + // draw console diagram |
| 106 | + set_multiply_color( color_white ); |
| 107 | + select_region( region_console ); |
| 108 | + draw_region_at( 400, 207 ); |
| 109 | + |
| 110 | + select_region( region_cartridge ); |
| 111 | + draw_region_at( 469, 76 ); |
| 112 | + |
| 113 | + select_region( region_down_arrow ); |
| 114 | + draw_region_at( 497, 149 ); |
| 115 | + |
| 116 | + // ensure everything gets drawn |
| 117 | + end_frame(); |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +// --------------------------------------------------------- |
| 122 | +// ERROR HANDLER FUNCTION |
| 123 | +// --------------------------------------------------------- |
| 124 | + |
| 125 | + |
| 126 | +void error_handler() |
| 127 | +{ |
| 128 | + // do not initialize these! |
| 129 | + // or else R0 will be overwritten |
| 130 | + int error_code; |
| 131 | + int instruction_pointer; |
| 132 | + int instruction; |
| 133 | + int immediate_value; |
| 134 | + |
| 135 | + // save registers to variables |
| 136 | + asm |
| 137 | + { |
| 138 | + "mov {error_code}, R0" |
| 139 | + "mov {instruction_pointer}, R1" |
| 140 | + "mov {instruction}, R2" |
| 141 | + "mov {immediate_value}, R3" |
| 142 | + } |
| 143 | + |
| 144 | + // ensure everything gets drawn |
| 145 | + end_frame(); |
| 146 | + |
| 147 | + // write the appropriate message for this error code |
| 148 | + if( error_code >= 0 && error_code < (int)error_unknown ) |
| 149 | + draw_message_screen( &error_messages[ error_code ] ); |
| 150 | + |
| 151 | + else |
| 152 | + draw_message_screen( &error_messages[ error_unknown ] ); |
| 153 | + |
| 154 | + // now print the related hex values |
| 155 | + set_multiply_color( error_colors_values ); |
| 156 | + print_hex_value( 49, 160, "Instruction Pointer", instruction_pointer ); |
| 157 | + print_hex_value( 49, 180, "Instruction", instruction ); |
| 158 | + print_hex_value( 49, 200, "Immediate Value", immediate_value ); |
| 159 | + |
| 160 | + // stop any sound |
| 161 | + stop_all_channels(); |
| 162 | +} |
| 163 | + |
| 164 | + |
| 165 | +// --------------------------------------------------------- |
| 166 | +// MAIN FUNCTION |
| 167 | +// --------------------------------------------------------- |
| 168 | + |
| 169 | +void main( void ) |
| 170 | +{ |
| 171 | + // very small wait before starting, |
| 172 | + // to ensure a black screen is seen |
| 173 | + clear_screen( color_black ); |
| 174 | + sleep( 15 ); |
| 175 | + |
| 176 | + // ------------------------------------ |
| 177 | + // PART 1: DEFINE ALL TEXTURE REGIONS |
| 178 | + // ------------------------------------ |
| 179 | + |
| 180 | + select_texture( -1 ); |
| 181 | + |
| 182 | + // all characters of the text font |
| 183 | + define_region_matrix( first_region_font, 1,1, 10,20, 1,1, 32,8, 0 ); |
| 184 | + |
| 185 | + // white pixel |
| 186 | + select_region( region_white_pixel ); |
| 187 | + define_region_topleft( 315,169, 315,169 ); |
| 188 | + |
| 189 | + // console |
| 190 | + select_region( region_console ); |
| 191 | + define_region_topleft( 1,164, 199,299 ); |
| 192 | + |
| 193 | + // cartridge |
| 194 | + select_region( region_cartridge ); |
| 195 | + define_region_topleft( 203,164, 284,237 ); |
| 196 | + |
| 197 | + // down arrow |
| 198 | + select_region( region_down_arrow ); |
| 199 | + define_region_topleft( 288,164, 311,223 ); |
| 200 | + |
| 201 | + // white square |
| 202 | + select_region( region_white_square ); |
| 203 | + define_region_topleft( 315,164, 316,165 ); |
| 204 | + |
| 205 | + // ------------------------------------ |
| 206 | + // PART 7: JUMP TO CARTRIDGE |
| 207 | + // ------------------------------------ |
| 208 | + |
| 209 | + // if no cartrige is connected, show an alert screen and stop |
| 210 | + if( !cartridge_connected() ) |
| 211 | + { |
| 212 | + request_cartridge(); |
| 213 | + exit(); |
| 214 | + } |
| 215 | + |
| 216 | + // ensure that any video parameters we might have used |
| 217 | + // are restored to their expected defaults at startup |
| 218 | + set_multiply_color( color_white ); |
| 219 | + select_region( 0 ); |
| 220 | + |
| 221 | + // jump to first position in cartridge program rom |
| 222 | + asm{ "jmp 0x20000000" } |
| 223 | +} |
0 commit comments