void glk_main(void) { FILE *f; int vb,no; Bottom = glk_window_open(0, 0, 0, wintype_TextBuffer, 1); if(Bottom == NULL) glk_exit(); glk_set_window(Bottom); if(game_file == NULL) Fatal("No game provided"); f = fopen(game_file, "r"); if(f==NULL) Fatal("Cannot open game"); if (Options & TRS80_STYLE) { Width = 64; TopHeight = 11; } else { Width = 80; TopHeight = 10; } if(split_screen) { Top = glk_window_open(Bottom, winmethod_Above | winmethod_Fixed, TopHeight, wintype_TextGrid, 0); if(Top == NULL) { split_screen = 0; Top = Bottom; } } else { Top = Bottom; } Output("\ Scott Free, A Scott Adams game driver in C.\n\ Release 1.14, (c) 1993,1994,1995 Swansea University Computer Society.\n\ Distributed under the GNU software license\n\n"); LoadDatabase(f,(Options&DEBUGGING)?1:0); fclose(f); srand(time(NULL)); while(1) { glk_tick(); PerformActions(0,0); Look(); if(GetInput(&vb,&no) == -1) continue; switch(PerformActions(vb,no)) { case -1:Output("I don't understand your command. "); break; case -2:Output("I can't do that yet. "); break; } /* Brian Howarth games seem to use -1 for forever */ if(Items[LIGHT_SOURCE].Location/*==-1*/!=DESTROYED && GameHeader.LightTime!= -1) { GameHeader.LightTime--; if(GameHeader.LightTime<1) { BitFlags|=(1<<LIGHTOUTBIT); if(Items[LIGHT_SOURCE].Location==CARRIED || Items[LIGHT_SOURCE].Location==MyLoc) { if(Options&SCOTTLIGHT) Output("Light has run out! "); else Output("Your light has run out. "); } if(Options&PREHISTORIC_LAMP) Items[LIGHT_SOURCE].Location=DESTROYED; } else if(GameHeader.LightTime<25) { if(Items[LIGHT_SOURCE].Location==CARRIED || Items[LIGHT_SOURCE].Location==MyLoc) { if(Options&SCOTTLIGHT) { Output("Light runs out in "); OutputNumber(GameHeader.LightTime); Output(" turns. "); } else { if(GameHeader.LightTime%5==0) Output("Your light is growing dim. "); } } } } } }
void gidispatch_call(glui32 funcnum, glui32 numargs, gluniversal_t *arglist) { gidispatch_function_t *gidispatch_function; char *prototype; int argument = 0; int slot = 0; gidispatch_function = gidispatch_get_function_by_id(funcnum); prototype = gidispatch_prototype(funcnum); printf("DEBUG: dispatch call name=%s, prototype=%s, numargs=%u -- ", gidispatch_function->name, prototype, (unsigned int)numargs); if (strcmp(prototype, "4IuIuIuIs") == 0) { printf("%u, %u, %u, %d\n", arglist[0].uint, arglist[1].uint, arglist[2].uint, arglist[3].sint); } else if (strcmp(prototype, "3IuIu:Iu") == 0) { printf("%u, %u, returning a glui32\n", arglist[0].uint, arglist[1].uint); } else if (strcmp(prototype, "3Qa<Iu:Qa") == 0) { printf("win at %p, outref to a glui32, returning a winid_t\n", arglist[0].opaqueref); } else if (strcmp(prototype, "3Qc<Iu:Qc") == 0) { printf("fileref at %p, outref to a glui32, returning a frefid_t\n", arglist[0].opaqueref); } else if (strcmp(prototype, "1Qa:") == 0) { printf("win at %p\n", arglist[0].opaqueref); } else if (strcmp(prototype, "6QaIuIuIuIu:Qa") == 0) { printf("win at %p, %u, %u, %u, %u, returning a winid_t\n", arglist[0].opaqueref, arglist[1].uint, arglist[2].uint, arglist[3].uint, arglist[4].uint); } else if (strcmp(prototype, "4IuIuIuIs:") == 0) { printf("%u, %u, %u, %d\n", arglist[0].uint, arglist[1].uint, arglist[2].uint, arglist[3].sint); } else if (strcmp(prototype, "1Qb:") == 0) { printf("stream at %p\n", arglist[0].opaqueref); } else if (strcmp(prototype, "1Iu:") == 0) { printf("%u\n", arglist[0].uint); } else if (strcmp(prototype, "2Qb<[2IuIu]:") == 0) { printf("stream at %p, some struct stuff here\n", arglist[0].opaqueref); } else if (strcmp(prototype, "3IuIuIu:") == 0) { printf("%u, %u, %u\n", arglist[0].uint, arglist[1].uint, arglist[2].uint); } else if (strcmp(prototype, "1:Qb") == 0) { printf("returning a strid_t\n"); } else if (strcmp(prototype, "4&+#!IuIuIu:Qb") == 0) { printf("retained, nonnull, array of glui32 at %p for length %u, %u, %u, returning a strid_t\n", arglist[1].array, arglist[2].uint, arglist[3].uint, arglist[4].uint); } else if (strcmp(prototype, "2Qc:Iu") == 0) { printf("fileref at %p, returning a glui32\n", arglist[0].opaqueref); } else if (strcmp(prototype, "1<+[4IuQaIuIu]:") == 0) { printf("some struct stuff here, nonnull\n"); } else if (strcmp(prototype, "1:Qb") == 0) { printf("returning a strid_t\n"); } else if (strcmp(prototype, "2Qb:Is") == 0) { printf("stream at %p, returning a glsi32\n", arglist[0].opaqueref); } else if (strcmp(prototype, "2Qc:Iu") == 0) { printf("fileref at %p, returning a glui32\n", arglist[0].opaqueref); } else if (strcmp(prototype, "3Qa&+#!CnIu:") == 0) { printf("win at %p, retained, nonnull, array of char at %p for length %u, %u\n", arglist[0].opaqueref, arglist[2].array, arglist[3].uint, arglist[4].uint); } else if (strcmp(prototype, "3Qb<Iu:Qb") == 0) { printf("stream at %p, outref to a glui32, returning a strid_t\n", arglist[0].opaqueref); } else if (strcmp(prototype, "4&+#!CnIuIu:Qb") == 0) { printf("retained, nonnull, array of char at %p for length %u, %u, %u, returning a strid_t\n", arglist[1].array, arglist[2].uint, arglist[3].uint, arglist[4].uint); } else if (strcmp(prototype, "4&+#!IuIuIu:Qb") == 0) { printf("retained, nonnull, array of glui32 at %p for length %u, %u, %u, returning a strid_t\n", arglist[1].array, arglist[2].uint, arglist[3].uint, arglist[4].uint); } else if (strcmp(prototype, "4IuSIu:Qc") == 0) { printf("%u, %s, %u, returning a frefid_t\n", arglist[0].uint, arglist[1].charstr, arglist[2].uint); } else if (strcmp(prototype, "4QcIuIu:Qb") == 0) { printf("fileref at %p, %u, %u, returning a strid_t\n", arglist[0].opaqueref, arglist[1].uint, arglist[2].uint); } else if (strcmp(prototype, "3Qa<Iu<Iu:") == 0) { printf("win at %p, outref to a glui32, outref to a glui32\n", arglist[0].opaqueref); } else if (strcmp(prototype, "6QaIuIsIsIuIu:") == 0) { printf("win at %p, %u, %d, %d, %u, %u\n", arglist[0].opaqueref, arglist[1].uint, arglist[2].sint, arglist[3].sint, arglist[4].uint, arglist[5].uint); } else if (strcmp(prototype, "4Iu<Iu<Iu:Iu") == 0) { printf("%u, outref to a glui32, outref to a glui32, returning a glui32\n", arglist[0].uint); } else { printf("unhandled prototype\n"); } switch (funcnum) { case 0x0004: /* gestalt */ arglist[3].uint = glk_gestalt(arglist[0].uint, arglist[1].uint); break; case 0x0005: /* gestalt_ext */ if (arglist[2].ptrflag) { arglist[6].uint = glk_gestalt_ext(arglist[0].uint, arglist[1].uint, arglist[3].array, arglist[4].uint); } else { arglist[4].uint = glk_gestalt_ext(arglist[0].uint, arglist[1].uint, NULL, 0); } break; case 0x0020: /* window_iterate */ if (arglist[1].ptrflag) arglist[4].opaqueref = glk_window_iterate(arglist[0].opaqueref, &arglist[2].uint); else arglist[3].opaqueref = glk_window_iterate(arglist[0].opaqueref, NULL); break; case 0x0023: /* window_open */ arglist[6].opaqueref = glk_window_open(arglist[0].opaqueref, arglist[1].uint, arglist[2].uint, arglist[3].uint, arglist[4].uint); break; case 0x0025: /* window_get_size */ { int ix = 1; glui32 *ptr1, *ptr2; if (!arglist[ix].ptrflag) { ptr1 = NULL; } else { ix++; ptr1 = &(arglist[ix].uint); } ix++; if (!arglist[ix].ptrflag) { ptr2 = NULL; } else { ix++; ptr2 = &(arglist[ix].uint); } ix++; glk_window_get_size(arglist[0].opaqueref, ptr1, ptr2); } break; case 0x0028: /* window_get_type */ arglist[2].uint = glk_window_get_type(arglist[0].opaqueref); break; case 0x002A: /* window_clear */ glk_window_clear(arglist[0].opaqueref); break; case 0x002B: /* window_move_cursor */ glk_window_move_cursor(arglist[0].opaqueref, arglist[1].uint, arglist[2].uint); break; case 0x002F: /* set_window */ glk_set_window(arglist[0].opaqueref); break; case 0x0040: /* stream_iterate */ if (arglist[1].ptrflag) arglist[4].opaqueref = glk_stream_iterate(arglist[0].opaqueref, &arglist[2].uint); else arglist[3].opaqueref = glk_stream_iterate(arglist[0].opaqueref, NULL); break; case 0x0042: /* stream_open_file */ arglist[4].opaqueref = glk_stream_open_file(arglist[0].opaqueref, arglist[1].uint, arglist[2].uint); break; case 0x0043: /* stream_open_memory */ if (arglist[0].ptrflag) arglist[6].opaqueref = glk_stream_open_memory(arglist[1].array, arglist[2].uint, arglist[3].uint, arglist[4].uint); else arglist[4].opaqueref = glk_stream_open_memory(NULL, 0, arglist[1].uint, arglist[2].uint); break; case 0x0044: /* stream_close */ if (arglist[1].ptrflag) { stream_result_t dat; glk_stream_close(arglist[0].opaqueref, &dat); arglist[2].uint = dat.readcount; arglist[3].uint = dat.writecount; } else { glk_stream_close(arglist[0].opaqueref, NULL); } break; case 0x0047: /* stream_set_current */ glk_stream_set_current(arglist[0].opaqueref); break; case 0x0048: /* stream_get_current */ arglist[1].opaqueref = glk_stream_get_current(); break; case 0x0061: /* fileref_create_by_name */ arglist[4].opaqueref = glk_fileref_create_by_name(arglist[0].uint, arglist[1].charstr, arglist[2].uint); break; case 0x0062: /* fileref_create_by_prompt */ arglist[4].opaqueref = glk_fileref_create_by_prompt(arglist[0].uint, arglist[1].uint, arglist[2].uint); break; case 0x0067: /* fileref_does_file_exist */ arglist[2].uint = glk_fileref_does_file_exist(arglist[0].opaqueref); break; case 0x0086: /* set_style */ glk_set_style(arglist[0].uint); break; case 0x0087: /* set_style_stream */ glk_set_style_stream(arglist[0].opaqueref, arglist[1].uint); break; case 0x0090: /* get_char_stream */ arglist[2].sint = glk_get_char_stream(arglist[0].opaqueref); break; case 0x00B0: /* stylehint_set */ glk_stylehint_set(arglist[0].uint, arglist[1].uint, arglist[2].uint, arglist[3].sint); break; case 0x00C0: /* select */ if (arglist[0].ptrflag) { event_t dat; glk_select(&dat); arglist[1].uint = dat.type; arglist[2].opaqueref = dat.win; arglist[3].uint = dat.val1; arglist[4].uint = dat.val2; } else { glk_select(NULL); } break; case 0x00D0: /* request_line_event */ if (arglist[1].ptrflag) glk_request_line_event(arglist[0].opaqueref, arglist[2].array, arglist[3].uint, arglist[4].uint); else glk_request_line_event(arglist[0].opaqueref, NULL, 0, arglist[2].uint); break; case 0x00D2: /* request_char_event */ glk_request_char_event(arglist[0].opaqueref); break; case 0x00E0: /* image_get_info */ { int ix = 1; glui32 *ptr1, *ptr2; if (!arglist[ix].ptrflag) { ptr1 = NULL; } else { ix++; ptr1 = &(arglist[ix].uint); } ix++; if (!arglist[ix].ptrflag) { ptr2 = NULL; } else { ix++; ptr2 = &(arglist[ix].uint); } ix++; ix++; arglist[ix].uint = glk_image_get_info(arglist[0].uint, ptr1, ptr2); } break; case 0x00E1: /* image_draw */ arglist[5].uint = glk_image_draw(arglist[0].opaqueref, arglist[1].uint, arglist[2].sint, arglist[3].sint); break; case 0x00E2: /* image_draw_scaled */ arglist[7].uint = glk_image_draw_scaled(arglist[0].opaqueref, arglist[1].uint, arglist[2].sint, arglist[3].sint, arglist[4].uint, arglist[5].uint); break; case 0x00EA: /* window_fill_rect */ glk_window_fill_rect(arglist[0].opaqueref, arglist[1].uint, arglist[2].sint, arglist[3].sint, arglist[4].uint, arglist[5].uint); break; case 0x0128: /* put_char_uni */ glk_put_char_uni(arglist[0].uint); break; case 0x0139: /* stream_open_memory_uni */ if (arglist[0].ptrflag) arglist[6].opaqueref = glk_stream_open_memory_uni(arglist[1].array, arglist[2].uint, arglist[3].uint, arglist[4].uint); else arglist[4].opaqueref = glk_stream_open_memory_uni(NULL, 0, arglist[1].uint, arglist[2].uint); break; default: printf("Unhandled call to %s via dispatch\n", gidispatch_function->name); #if 0 case 0x0001: /* exit */ glk_exit(); break; case 0x0002: /* set_interrupt_handler */ /* cannot be invoked through dispatch layer */ break; case 0x0003: /* tick */ glk_tick(); break; case 0x0021: /* window_get_rock */ arglist[2].uint = glk_window_get_rock(arglist[0].opaqueref); break; case 0x0022: /* window_get_root */ arglist[1].opaqueref = glk_window_get_root(); break; case 0x0024: /* window_close */ if (arglist[1].ptrflag) { stream_result_t dat; glk_window_close(arglist[0].opaqueref, &dat); arglist[2].uint = dat.readcount; arglist[3].uint = dat.writecount; } else { glk_window_close(arglist[0].opaqueref, NULL); } break; case 0x0026: /* window_set_arrangement */ glk_window_set_arrangement(arglist[0].opaqueref, arglist[1].uint, arglist[2].uint, arglist[3].opaqueref); break; case 0x0027: /* window_get_arrangement */ { int ix = 1; glui32 *ptr1, *ptr2; winid_t *ptr3; if (!arglist[ix].ptrflag) { ptr1 = NULL; } else { ix++; ptr1 = &(arglist[ix].uint); } ix++; if (!arglist[ix].ptrflag) { ptr2 = NULL; } else { ix++; ptr2 = &(arglist[ix].uint); } ix++; if (!arglist[ix].ptrflag) { ptr3 = NULL; } else { ix++; ptr3 = (winid_t *)(&(arglist[ix].opaqueref)); } ix++; glk_window_get_arrangement(arglist[0].opaqueref, ptr1, ptr2, ptr3); } break; case 0x0029: /* window_get_parent */ arglist[2].opaqueref = glk_window_get_parent(arglist[0].opaqueref); break; case 0x002C: /* window_get_stream */ arglist[2].opaqueref = glk_window_get_stream(arglist[0].opaqueref); break; case 0x002D: /* window_set_echo_stream */ glk_window_set_echo_stream(arglist[0].opaqueref, arglist[1].opaqueref); break; case 0x002E: /* window_get_echo_stream */ arglist[2].opaqueref = glk_window_get_echo_stream(arglist[0].opaqueref); break; case 0x0030: /* window_get_sibling */ arglist[2].opaqueref = glk_window_get_sibling(arglist[0].opaqueref); break; case 0x0041: /* stream_get_rock */ arglist[2].uint = glk_stream_get_rock(arglist[0].opaqueref); break; case 0x0045: /* stream_set_position */ glk_stream_set_position(arglist[0].opaqueref, arglist[1].sint, arglist[2].uint); break; case 0x0046: /* stream_get_position */ arglist[2].uint = glk_stream_get_position(arglist[0].opaqueref); break; case 0x0048: /* stream_get_current */ arglist[1].opaqueref = glk_stream_get_current(); break; case 0x0060: /* fileref_create_temp */ arglist[3].opaqueref = glk_fileref_create_temp(arglist[0].uint, arglist[1].uint); break; case 0x0063: /* fileref_destroy */ glk_fileref_destroy(arglist[0].opaqueref); break; case 0x0064: /* fileref_iterate */ if (arglist[1].ptrflag) arglist[4].opaqueref = glk_fileref_iterate(arglist[0].opaqueref, &arglist[2].uint); else arglist[3].opaqueref = glk_fileref_iterate(arglist[0].opaqueref, NULL); break; case 0x0065: /* fileref_get_rock */ arglist[2].uint = glk_fileref_get_rock(arglist[0].opaqueref); break; case 0x0066: /* fileref_delete_file */ glk_fileref_delete_file(arglist[0].opaqueref); break; case 0x0068: /* fileref_create_from_fileref */ arglist[4].opaqueref = glk_fileref_create_from_fileref(arglist[0].uint, arglist[1].opaqueref, arglist[2].uint); break; case 0x0080: /* put_char */ glk_put_char(arglist[0].uch); break; case 0x0081: /* put_char_stream */ glk_put_char_stream(arglist[0].opaqueref, arglist[1].uch); break; case 0x0082: /* put_string */ glk_put_string(arglist[0].charstr); break; case 0x0083: /* put_string_stream */ glk_put_string_stream(arglist[0].opaqueref, arglist[1].charstr); break; case 0x0084: /* put_buffer */ if (arglist[0].ptrflag) glk_put_buffer(arglist[1].array, arglist[2].uint); else glk_put_buffer(NULL, 0); break; case 0x0085: /* put_buffer_stream */ if (arglist[1].ptrflag) glk_put_buffer_stream(arglist[0].opaqueref, arglist[2].array, arglist[3].uint); else glk_put_buffer_stream(arglist[0].opaqueref, NULL, 0); break; case 0x0091: /* get_line_stream */ if (arglist[1].ptrflag) arglist[5].uint = glk_get_line_stream(arglist[0].opaqueref, arglist[2].array, arglist[3].uint); else arglist[3].uint = glk_get_line_stream(arglist[0].opaqueref, NULL, 0); break; case 0x0092: /* get_buffer_stream */ if (arglist[1].ptrflag) arglist[5].uint = glk_get_buffer_stream(arglist[0].opaqueref, arglist[2].array, arglist[3].uint); else arglist[3].uint = glk_get_buffer_stream(arglist[0].opaqueref, NULL, 0); break; case 0x00A0: /* char_to_lower */ arglist[2].uch = glk_char_to_lower(arglist[0].uch); break; case 0x00A1: /* char_to_upper */ arglist[2].uch = glk_char_to_upper(arglist[0].uch); break; case 0x00B1: /* stylehint_clear */ glk_stylehint_clear(arglist[0].uint, arglist[1].uint, arglist[2].uint); break; case 0x00B2: /* style_distinguish */ arglist[4].uint = glk_style_distinguish(arglist[0].opaqueref, arglist[1].uint, arglist[2].uint); break; case 0x00B3: /* style_measure */ if (arglist[3].ptrflag) arglist[6].uint = glk_style_measure(arglist[0].opaqueref, arglist[1].uint, arglist[2].uint, &(arglist[4].uint)); else arglist[5].uint = glk_style_measure(arglist[0].opaqueref, arglist[1].uint, arglist[2].uint, NULL); break; case 0x00C1: /* select_poll */ if (arglist[0].ptrflag) { event_t dat; glk_select_poll(&dat); arglist[1].uint = dat.type; arglist[2].opaqueref = dat.win; arglist[3].uint = dat.val1; arglist[4].uint = dat.val2; } else { glk_select_poll(NULL); } break; case 0x00D1: /* cancel_line_event */ if (arglist[1].ptrflag) { event_t dat; glk_cancel_line_event(arglist[0].opaqueref, &dat); arglist[2].uint = dat.type; arglist[3].opaqueref = dat.win; arglist[4].uint = dat.val1; arglist[5].uint = dat.val2; } else { glk_cancel_line_event(arglist[0].opaqueref, NULL); } break; case 0x00D3: /* cancel_char_event */ glk_cancel_char_event(arglist[0].opaqueref); break; case 0x00D4: /* request_mouse_event */ glk_request_mouse_event(arglist[0].opaqueref); break; case 0x00D5: /* cancel_mouse_event */ glk_cancel_mouse_event(arglist[0].opaqueref); break; case 0x00D6: /* request_timer_events */ glk_request_timer_events(arglist[0].uint); break; #ifdef GLK_MODULE_IMAGE case 0x00E8: /* window_flow_break */ glk_window_flow_break(arglist[0].opaqueref); break; case 0x00E9: /* window_erase_rect */ glk_window_erase_rect(arglist[0].opaqueref, arglist[1].sint, arglist[2].sint, arglist[3].uint, arglist[4].uint); break; case 0x00EB: /* window_set_background_color */ glk_window_set_background_color(arglist[0].opaqueref, arglist[1].uint); break; #endif /* GLK_MODULE_IMAGE */ #ifdef GLK_MODULE_SOUND case 0x00F0: /* schannel_iterate */ if (arglist[1].ptrflag) arglist[4].opaqueref = glk_schannel_iterate(arglist[0].opaqueref, &arglist[2].uint); else arglist[3].opaqueref = glk_schannel_iterate(arglist[0].opaqueref, NULL); break; case 0x00F1: /* schannel_get_rock */ arglist[2].uint = glk_schannel_get_rock(arglist[0].opaqueref); break; case 0x00F2: /* schannel_create */ arglist[2].opaqueref = glk_schannel_create(arglist[0].uint); break; case 0x00F3: /* schannel_destroy */ glk_schannel_destroy(arglist[0].opaqueref); break; case 0x00F8: /* schannel_play */ arglist[3].uint = glk_schannel_play(arglist[0].opaqueref, arglist[1].uint); break; case 0x00F9: /* schannel_play_ext */ arglist[5].uint = glk_schannel_play_ext(arglist[0].opaqueref, arglist[1].uint, arglist[2].uint, arglist[3].uint); break; case 0x00FA: /* schannel_stop */ glk_schannel_stop(arglist[0].opaqueref); break; case 0x00FB: /* schannel_set_volume */ glk_schannel_set_volume(arglist[0].opaqueref, arglist[1].uint); break; case 0x00FC: /* sound_load_hint */ glk_sound_load_hint(arglist[0].uint, arglist[1].uint); break; #endif /* GLK_MODULE_SOUND */ #ifdef GLK_MODULE_HYPERLINKS case 0x0100: /* set_hyperlink */ glk_set_hyperlink(arglist[0].uint); break; case 0x0101: /* set_hyperlink_stream */ glk_set_hyperlink_stream(arglist[0].opaqueref, arglist[1].uint); break; case 0x0102: /* request_hyperlink_event */ glk_request_hyperlink_event(arglist[0].opaqueref); break; case 0x0103: /* cancel_hyperlink_event */ glk_cancel_hyperlink_event(arglist[0].opaqueref); break; #endif /* GLK_MODULE_HYPERLINKS */ #ifdef GLK_MODULE_UNICODE case 0x0120: /* buffer_to_lower_case_uni */ if (arglist[0].ptrflag) arglist[5].uint = glk_buffer_to_lower_case_uni(arglist[1].array, arglist[2].uint, arglist[3].uint); else arglist[3].uint = glk_buffer_to_lower_case_uni(NULL, 0, arglist[1].uint); break; case 0x0121: /* buffer_to_upper_case_uni */ if (arglist[0].ptrflag) arglist[5].uint = glk_buffer_to_upper_case_uni(arglist[1].array, arglist[2].uint, arglist[3].uint); else arglist[3].uint = glk_buffer_to_upper_case_uni(NULL, 0, arglist[1].uint); break; case 0x0122: /* buffer_to_title_case_uni */ if (arglist[0].ptrflag) arglist[6].uint = glk_buffer_to_title_case_uni(arglist[1].array, arglist[2].uint, arglist[3].uint, arglist[4].uint); else arglist[4].uint = glk_buffer_to_title_case_uni(NULL, 0, arglist[1].uint, arglist[2].uint); break; case 0x0129: /* put_string_uni */ glk_put_string_uni(arglist[0].unicharstr); break; case 0x012A: /* put_buffer_uni */ if (arglist[0].ptrflag) glk_put_buffer_uni(arglist[1].array, arglist[2].uint); else glk_put_buffer_uni(NULL, 0); break; case 0x012B: /* put_char_stream_uni */ glk_put_char_stream_uni(arglist[0].opaqueref, arglist[1].uint); break; case 0x012C: /* put_string_stream_uni */ glk_put_string_stream_uni(arglist[0].opaqueref, arglist[1].unicharstr); break; case 0x012D: /* put_buffer_stream_uni */ if (arglist[1].ptrflag) glk_put_buffer_stream_uni(arglist[0].opaqueref, arglist[2].array, arglist[3].uint); else glk_put_buffer_stream_uni(arglist[0].opaqueref, NULL, 0); break; case 0x0130: /* get_char_stream_uni */ arglist[2].sint = glk_get_char_stream_uni(arglist[0].opaqueref); break; case 0x0131: /* get_buffer_stream_uni */ if (arglist[1].ptrflag) arglist[5].uint = glk_get_buffer_stream_uni(arglist[0].opaqueref, arglist[2].array, arglist[3].uint); else arglist[3].uint = glk_get_buffer_stream_uni(arglist[0].opaqueref, NULL, 0); break; case 0x0132: /* get_line_stream_uni */ if (arglist[1].ptrflag) arglist[5].uint = glk_get_line_stream_uni(arglist[0].opaqueref, arglist[2].array, arglist[3].uint); else arglist[3].uint = glk_get_line_stream_uni(arglist[0].opaqueref, NULL, 0); break; case 0x0138: /* stream_open_file_uni */ arglist[4].opaqueref = glk_stream_open_file_uni(arglist[0].opaqueref, arglist[1].uint, arglist[2].uint); break; case 0x0140: /* request_char_event_uni */ glk_request_char_event_uni(arglist[0].opaqueref); break; case 0x0141: /* request_line_event_uni */ if (arglist[1].ptrflag) glk_request_line_event_uni(arglist[0].opaqueref, arglist[2].array, arglist[3].uint, arglist[4].uint); else glk_request_line_event_uni(arglist[0].opaqueref, NULL, 0, arglist[2].uint); break; #endif /* GLK_MODULE_UNICODE */ #endif /* 0 */ } }
void process_instructions(void) { if(njumps <= ++ilevel) { jumps = realloc(jumps, ++njumps * sizeof *jumps); if(jumps == NULL) die("unable to allocate memory for jump buffer"); } switch(setjmp(jumps[ilevel])) { case 1: /* Normal break from interrupt. */ return; case 2: /* Special break: a restart was requested. */ { /* §6.1.3: Flags2 is preserved on a restart. */ uint16_t flags2 = WORD(0x10); process_story(); STORE_WORD(0x10, flags2); } break; } while(1) { uint8_t opcode; #if defined(ZTERP_GLK) && defined(ZTERP_GLK_TICK) glk_tick(); #endif ZPC(pc); opcode = BYTE(pc++); /* long 2OP */ if(opcode < 0x80) { znargs = 2; if(opcode & 0x40) zargs[0] = variable(BYTE(pc++)); else zargs[0] = BYTE(pc++); if(opcode & 0x20) zargs[1] = variable(BYTE(pc++)); else zargs[1] = BYTE(pc++); op_call(TWO, opcode & 0x1f); } /* short 1OP */ else if(opcode < 0xb0) { znargs = 1; if(opcode < 0x90) /* large constant */ { zargs[0] = WORD(pc); pc += 2; } else if(opcode < 0xa0) /* small constant */ { zargs[0] = BYTE(pc++); } else /* variable */ { zargs[0] = variable(BYTE(pc++)); } op_call(ONE, opcode & 0x0f); } /* short 0OP (plus EXT) */ else if(opcode < 0xc0) { znargs = 0; op_call(ZERO, opcode & 0x0f); } /* variable 2OP */ else if(opcode < 0xe0) { znargs = 0; decode_var(BYTE(pc++)); op_call(TWO, opcode & 0x1f); } /* Double variable VAR */ else if(opcode == 0xec || opcode == 0xfa) { uint8_t types1, types2; znargs = 0; types1 = BYTE(pc++); types2 = BYTE(pc++); decode_var(types1); decode_var(types2); op_call(VAR, opcode & 0x1f); } /* variable VAR */ else { znargs = 0; read_pc = pc - 1; decode_var(BYTE(pc++)); op_call(VAR, opcode & 0x1f); } } }
void decode(void) { unsigned optypes; int maxoperands; while(!exit_decoder) { #ifdef ANDGLK extern int do_autosave; extern char AUTOSAVE_FILE[]; extern offset stack_pointer; extern zword *local_vars; extern zword frame_count; offset save_pcp = PC; offset save_oldpcp = oldPC; offset save_sp = stack_pointer; zword* save_fp = local_vars; zword save_optypes = optypes; zword save_maxoperands = maxoperands; int save_frame_count = frame_count; zword save_zargs[8] = { operand[0], operand[1], operand[2], operand[3], operand[4], operand[5], operand[6], operand[7] }; int zarg_ix, save_zargc = 0; //zargc; #endif zbyte opcode = HIBYTE(PC); //LOGD("state: PC:%x op:%x", (glui32)PC, (glui32)opcode); oldPC = PC; #ifdef DEBUGGING if(do_check_watches) check_watches(); #endif PC++; #ifndef NO_TICK glk_tick(); /* tick tock hickery dock the mouse ran up the clock */ #endif /* Top bits decide opcode/operand encoding */ switch(opcode >> 4) { case 0: case 1: /* long 2OP */ operand[0] = HIBYTE(PC); /* small constant */ operand[1] = HIBYTE(PC+1); /* small constant */ numoperands = 2; PC += 2; opcode += OFFSET_2OP - 0x00; break; case 2: case 3: /* long 2OP */ operand[0] = HIBYTE(PC); /* small constant */ operand[1] = get_var(HIBYTE(PC+1)); /* variable */ numoperands = 2; PC += 2; opcode += OFFSET_2OP - 0x20; break; case 4: case 5: /* long 2OP */ operand[0] = get_var(HIBYTE(PC)); /* variable */ operand[1] = HIBYTE(PC+1); /* small constant */ numoperands = 2; PC += 2; opcode += OFFSET_2OP - 0x40; break; case 6: case 7: /* long 2OP */ operand[0] = get_var(HIBYTE(PC)); /* variable */ operand[1] = get_var(HIBYTE(PC+1)); /* variable */ numoperands = 2; PC += 2; opcode += OFFSET_2OP - 0x60; break; case 8: /* short 1OP */ operand[0] = HIWORD(PC); /* large constant */ numoperands = 1; PC += ZWORD_SIZE; opcode += OFFSET_1OP - 0x80; break; case 9: /* short 1OP */ operand[0] = HIBYTE(PC); /* small constant */ numoperands = 1; PC += 1; opcode += OFFSET_1OP - 0x90; break; case 10: /* short 1OP */ operand[0] = get_var(HIBYTE(PC)); /* variable */ numoperands = 1; PC += 1; opcode += OFFSET_1OP - 0xa0; break; case 11: /* short 0OP */ if(opcode != 0xbe) { numoperands = 0; opcode += OFFSET_0OP - 0xb0; break; } /* EXTENDED */ opcode = HIBYTE(PC); /* Get the extended opcode */ optypes = HIBYTE(PC+1); PC += 2; #ifndef FAST if(OFFSET_EXT + opcode > OFFSET_END) { n_show_error(E_INSTR, "unknown extended opcode", opcode); break; } #endif for(numoperands = 0; numoperands < 4; numoperands++) { switch(optypes & (3 << 6)) { /* Look at the highest two bits. */ case 0 << 6: operand[numoperands] = HIWORD(PC); PC+=ZWORD_SIZE; break; case 1 << 6: operand[numoperands] = HIBYTE(PC); PC++; break; case 2 << 6: operand[numoperands] = get_var(HIBYTE(PC)); PC++; break; default: goto END_OF_EXTENDED; /* inky says, "use the goto." */ } optypes <<= 2; /* Move the next two bits into position. */ } END_OF_EXTENDED: opcode += OFFSET_EXT; break; case 12: case 13: case 14: case 15: /* variable operand count */ maxoperands = 4; optypes = ((unsigned) HIBYTE(PC)) << 8; /* Shift left so our loop will */ /* be the same for both 4 and 8 operands */ PC++; if(opcode == 0xec || opcode == 0xfa) { /* call_vs2 and call_vn2 */ maxoperands = 8; optypes |= HIBYTE(PC); /* Fill the bottom 8 bits */ PC++; } for(numoperands = 0; numoperands < maxoperands; numoperands++) { switch(optypes & (3 << 14)) { /* Look at the highest two bits. */ case 0 << 14: operand[numoperands] = HIWORD(PC); PC+=ZWORD_SIZE; break; case 1 << 14: operand[numoperands] = HIBYTE(PC); PC++; break; case 2 << 14: operand[numoperands] = get_var(HIBYTE(PC)); PC++; break; default: goto END_OF_VARIABLE; } optypes <<= 2; /* Move the next two bits into position. */ } END_OF_VARIABLE: opcode += OFFSET_2OP - 0xc0; break; } opcodetable[opcode](); #ifdef ANDGLK if (do_autosave) { PC = save_pcp; oldPC = save_oldpcp; stack_pointer = save_sp; local_vars = save_fp; numoperands = save_zargc; frame_count = save_frame_count; for (zarg_ix=0; zarg_ix<8; ++zarg_ix) operand[zarg_ix] = save_zargs[zarg_ix]; optypes = save_optypes; maxoperands = save_maxoperands; savegame(); do_autosave = 0; AUTOSAVE_FILE[0] = '\0'; continue; } #endif } }