From e786b6f53652f26aaba0e9da5b5f2863faa3bda6 Mon Sep 17 00:00:00 2001 From: David Griffith Date: Sat, 21 Sep 2019 17:53:32 -0700 Subject: [PATCH] Convert process.c to K&R style. --- src/common/process.c | 892 ++++++++++++++++++++----------------------- 1 file changed, 411 insertions(+), 481 deletions(-) diff --git a/src/common/process.c b/src/common/process.c index 86e3929..b5750ad 100644 --- a/src/common/process.c +++ b/src/common/process.c @@ -24,150 +24,149 @@ #include "djfrotz.h" #endif - zword zargs[8]; int zargc; static int finished = 0; -static void __extended__ (void); -static void __illegal__ (void); - -void (*op0_opcodes[0x10]) (void) = { - z_rtrue, - z_rfalse, - z_print, - z_print_ret, - z_nop, - z_save, - z_restore, - z_restart, - z_ret_popped, - z_catch, - z_quit, - z_new_line, - z_show_status, - z_verify, - __extended__, - z_piracy +static void __extended__(void); +static void __illegal__(void); + +void (*op0_opcodes[0x10])(void) = { + z_rtrue, + z_rfalse, + z_print, + z_print_ret, + z_nop, + z_save, + z_restore, + z_restart, + z_ret_popped, + z_catch, + z_quit, + z_new_line, + z_show_status, + z_verify, + __extended__, + z_piracy }; -void (*op1_opcodes[0x10]) (void) = { - z_jz, - z_get_sibling, - z_get_child, - z_get_parent, - z_get_prop_len, - z_inc, - z_dec, - z_print_addr, - z_call_s, - z_remove_obj, - z_print_obj, - z_ret, - z_jump, - z_print_paddr, - z_load, - z_call_n +void (*op1_opcodes[0x10])(void) = { + z_jz, + z_get_sibling, + z_get_child, + z_get_parent, + z_get_prop_len, + z_inc, + z_dec, + z_print_addr, + z_call_s, + z_remove_obj, + z_print_obj, + z_ret, + z_jump, + z_print_paddr, + z_load, + z_call_n }; -void (*var_opcodes[0x40]) (void) = { - __illegal__, - z_je, - z_jl, - z_jg, - z_dec_chk, - z_inc_chk, - z_jin, - z_test, - z_or, - z_and, - z_test_attr, - z_set_attr, - z_clear_attr, - z_store, - z_insert_obj, - z_loadw, - z_loadb, - z_get_prop, - z_get_prop_addr, - z_get_next_prop, - z_add, - z_sub, - z_mul, - z_div, - z_mod, - z_call_s, - z_call_n, - z_set_colour, - z_throw, - __illegal__, - __illegal__, - __illegal__, - z_call_s, - z_storew, - z_storeb, - z_put_prop, - z_read, - z_print_char, - z_print_num, - z_random, - z_push, - z_pull, - z_split_window, - z_set_window, - z_call_s, - z_erase_window, - z_erase_line, - z_set_cursor, - z_get_cursor, - z_set_text_style, - z_buffer_mode, - z_output_stream, - z_input_stream, - z_sound_effect, - z_read_char, - z_scan_table, - z_not, - z_call_n, - z_call_n, - z_tokenise, - z_encode_text, - z_copy_table, - z_print_table, - z_check_arg_count +void (*var_opcodes[0x40])(void) = { + __illegal__, + z_je, + z_jl, + z_jg, + z_dec_chk, + z_inc_chk, + z_jin, + z_test, + z_or, + z_and, + z_test_attr, + z_set_attr, + z_clear_attr, + z_store, + z_insert_obj, + z_loadw, + z_loadb, + z_get_prop, + z_get_prop_addr, + z_get_next_prop, + z_add, + z_sub, + z_mul, + z_div, + z_mod, + z_call_s, + z_call_n, + z_set_colour, + z_throw, + __illegal__, + __illegal__, + __illegal__, + z_call_s, + z_storew, + z_storeb, + z_put_prop, + z_read, + z_print_char, + z_print_num, + z_random, + z_push, + z_pull, + z_split_window, + z_set_window, + z_call_s, + z_erase_window, + z_erase_line, + z_set_cursor, + z_get_cursor, + z_set_text_style, + z_buffer_mode, + z_output_stream, + z_input_stream, + z_sound_effect, + z_read_char, + z_scan_table, + z_not, + z_call_n, + z_call_n, + z_tokenise, + z_encode_text, + z_copy_table, + z_print_table, + z_check_arg_count }; -void (*ext_opcodes[0x1d]) (void) = { - z_save, - z_restore, - z_log_shift, - z_art_shift, - z_set_font, - z_draw_picture, - z_picture_data, - z_erase_picture, - z_set_margins, - z_save_undo, - z_restore_undo, - z_print_unicode, - z_check_unicode, - __illegal__, - __illegal__, - __illegal__, - z_move_window, - z_window_size, - z_window_style, - z_get_wind_prop, - z_scroll_window, - z_pop_stack, - z_read_mouse, - z_mouse_window, - z_push_stack, - z_put_wind_prop, - z_print_form, - z_make_menu, - z_picture_table +void (*ext_opcodes[0x1d])(void) = { + z_save, + z_restore, + z_log_shift, + z_art_shift, + z_set_font, + z_draw_picture, + z_picture_data, + z_erase_picture, + z_set_margins, + z_save_undo, + z_restore_undo, + z_print_unicode, + z_check_unicode, + __illegal__, + __illegal__, + __illegal__, + z_move_window, + z_window_size, + z_window_style, + z_get_wind_prop, + z_scroll_window, + z_pop_stack, + z_read_mouse, + z_mouse_window, + z_push_stack, + z_put_wind_prop, + z_print_form, + z_make_menu, + z_picture_table }; @@ -177,48 +176,43 @@ void (*ext_opcodes[0x1d]) (void) = { * Initialize process variables. * */ -void init_process (void) +void init_process(void) { - finished = 0; + finished = 0; } + /* * load_operand * * Load an operand, either a variable or a constant. * */ -static void load_operand (zbyte type) +static void load_operand(zbyte type) { - zword value; - - if (type & 2) { /* variable */ - - zbyte variable; - - CODE_BYTE (variable) - - if (variable == 0) - value = *sp++; - else if (variable < 16) - value = *(fp - variable); - else { - zword addr = h_globals + 2 * (variable - 16); - LOW_WORD (addr, value) - } - - } else if (type & 1) { /* small constant */ - - zbyte bvalue; - - CODE_BYTE (bvalue) - value = bvalue; - - } else CODE_WORD (value) /* large constant */ - - zargs[zargc++] = value; - -}/* load_operand */ + zword value; + + if (type & 2) { /* variable */ + zbyte variable; + + CODE_BYTE(variable) + if (variable == 0) + value = *sp++; + else if (variable < 16) + value = *(fp - variable); + else { + zword addr = h_globals + 2 * (variable - 16); + LOW_WORD(addr, value) + } + } else if (type & 1) { /* small constant */ + zbyte bvalue; + + CODE_BYTE(bvalue) + value = bvalue; + } else + CODE_WORD(value) /* large constant */ + zargs[zargc++] = value; +} /* load_operand */ /* @@ -228,22 +222,18 @@ static void load_operand (zbyte type) * for a VAR or EXT opcode. * */ -static void load_all_operands (zbyte specifier) +static void load_all_operands(zbyte specifier) { - int i; - - for (i = 6; i >= 0; i -= 2) { - - zbyte type = (specifier >> i) & 0x03; - - if (type == 3) - break; - - load_operand (type); + int i; - } + for (i = 6; i >= 0; i -= 2) { + zbyte type = (specifier >> i) & 0x03; -}/* load_all_operands */ + if (type == 3) + break; + load_operand(type); + } +} /* load_all_operands */ /* @@ -252,69 +242,54 @@ static void load_all_operands (zbyte specifier) * Z-code interpreter main loop * */ -void interpret (void) +void interpret(void) { - /* If we got a save file on the command line, use it now. */ - if(f_setup.restore_mode==1) { - z_restore(); - f_setup.restore_mode=0; - } - - do { - - zbyte opcode; - - CODE_BYTE (opcode) - - zargc = 0; - - if (opcode < 0x80) { /* 2OP opcodes */ - - load_operand ((zbyte) (opcode & 0x40) ? 2 : 1); - load_operand ((zbyte) (opcode & 0x20) ? 2 : 1); - - var_opcodes[opcode & 0x1f] (); - - } else if (opcode < 0xb0) { /* 1OP opcodes */ - - load_operand ((zbyte) (opcode >> 4)); - - op1_opcodes[opcode & 0x0f] (); - - } else if (opcode < 0xc0) { /* 0OP opcodes */ - - op0_opcodes[opcode - 0xb0] (); - - } else { /* VAR opcodes */ - - zbyte specifier1; - zbyte specifier2; - - if (opcode == 0xec || opcode == 0xfa) { /* opcodes 0xec */ - CODE_BYTE (specifier1) /* and 0xfa are */ - CODE_BYTE (specifier2) /* call opcodes */ - load_all_operands (specifier1); /* with up to 8 */ - load_all_operands (specifier2); /* arguments */ - } else { - CODE_BYTE (specifier1) - load_all_operands (specifier1); - } - - var_opcodes[opcode - 0xc0] (); - + /* If we got a save file on the command line, use it now. */ + if (f_setup.restore_mode == 1) { + z_restore(); + f_setup.restore_mode = 0; } + do { + zbyte opcode; + + CODE_BYTE(opcode) + zargc = 0; + + if (opcode < 0x80) { /* 2OP opcodes */ + load_operand((zbyte) (opcode & 0x40) ? 2 : 1); + load_operand((zbyte) (opcode & 0x20) ? 2 : 1); + var_opcodes[opcode & 0x1f] (); + } else if (opcode < 0xb0) { /* 1OP opcodes */ + load_operand((zbyte) (opcode >> 4)); + op1_opcodes[opcode & 0x0f] (); + } else if (opcode < 0xc0) { /* 0OP opcodes */ + op0_opcodes[opcode - 0xb0] (); + } else { /* VAR opcodes */ + zbyte specifier1; + zbyte specifier2; + if (opcode == 0xec || opcode == 0xfa) { /* opcodes 0xec */ + CODE_BYTE(specifier1) /* and 0xfa are */ + CODE_BYTE(specifier2) /* call opcodes */ + load_all_operands(specifier1); /* with up to 8 */ + load_all_operands(specifier2); /* arguments */ + } else { + CODE_BYTE(specifier1) + load_all_operands(specifier1); + } + var_opcodes[opcode - 0xc0] (); + } + #if defined(DJGPP) && defined(SOUND_SUPPORT) - if (end_of_sound_flag) - end_of_sound (); + if (end_of_sound_flag) + end_of_sound(); #endif - os_tick(); - } while (finished == 0); - - finished--; + os_tick(); + } while (finished == 0); -}/* interpret */ + finished--; +} /* interpret */ /* @@ -326,70 +301,60 @@ void interpret (void) * can be 0 (z_call_s), 1 (z_call_n) or 2 (direct call). * */ -void call (zword routine, int argc, zword *args, int ct) +void call(zword routine, int argc, zword * args, int ct) { - long pc; - zword value; - zbyte count; - int i; - - if (sp - stack < 4) - runtime_error (ERR_STK_OVF); - - GET_PC (pc) - - *--sp = (zword) (pc >> 9); - *--sp = (zword) (pc & 0x1ff); - *--sp = (zword) (fp - stack - 1); - *--sp = (zword) (argc | (ct << 12)); - - fp = sp; - frame_count++; - - /* Calculate byte address of routine */ - - if (h_version <= V3) - pc = (long) routine << 1; - else if (h_version <= V5) - pc = (long) routine << 2; - else if (h_version <= V7) - pc = ((long) routine << 2) + ((long) h_functions_offset << 3); - else /* h_version == V8 */ - pc = (long) routine << 3; - - if (pc >= story_size) - runtime_error (ERR_ILL_CALL_ADDR); - - SET_PC (pc) - - /* Initialise local variables */ - - CODE_BYTE (count) - - if (count > 15) - runtime_error (ERR_CALL_NON_RTN); - if (sp - stack < count) - runtime_error (ERR_STK_OVF); - - fp[0] |= (zword) count << 8; /* Save local var count for Quetzal. */ - - value = 0; - - for (i = 0; i < count; i++) { - - if (h_version <= V4) /* V1 to V4 games provide default */ - CODE_WORD (value) /* values for all local variables */ - - *--sp = (zword) ((argc-- > 0) ? args[i] : value); - - } - - /* Start main loop for direct calls */ - - if (ct == 2) - interpret (); + long pc; + zword value; + zbyte count; + int i; + + if (sp - stack < 4) + runtime_error(ERR_STK_OVF); + + GET_PC(pc) + * --sp = (zword) (pc >> 9); + *--sp = (zword) (pc & 0x1ff); + *--sp = (zword) (fp - stack - 1); + *--sp = (zword) (argc | (ct << 12)); + + fp = sp; + frame_count++; + + /* Calculate byte address of routine */ + + if (h_version <= V3) + pc = (long)routine << 1; + else if (h_version <= V5) + pc = (long)routine << 2; + else if (h_version <= V7) + pc = ((long)routine << 2) + ((long)h_functions_offset << 3); + else /* h_version == V8 */ + pc = (long)routine << 3; + + if (pc >= story_size) + runtime_error(ERR_ILL_CALL_ADDR); + + SET_PC(pc) + /* Initialise local variables */ + CODE_BYTE(count) + + if (count > 15) + runtime_error(ERR_CALL_NON_RTN); + if (sp - stack < count) + runtime_error(ERR_STK_OVF); + + fp[0] |= (zword) count << 8; /* Save local var count for Quetzal. */ + value = 0; + for (i = 0; i < count; i++) { + if (h_version <= V4) /* V1 to V4 games provide default */ + CODE_WORD(value) /* values for all local variables */ + *--sp = (zword) ((argc-- > 0) ? args[i] : value); + } -}/* call */ + /* Start main loop for direct calls */ + if (ct == 2) + interpret(); +} /* call */ /* @@ -401,38 +366,33 @@ void call (zword routine, int argc, zword *args, int ct) * and we must exit the interpreter loop. * */ -void ret (zword value) +void ret(zword value) { - long pc; - int ct; - - if (sp > fp) - runtime_error (ERR_STK_UNDF); + long pc; + int ct; - sp = fp; + if (sp > fp) + runtime_error(ERR_STK_UNDF); - ct = *sp++ >> 12; - frame_count--; - fp = stack + 1 + *sp++; - pc = *sp++; - pc = ((long) *sp++ << 9) | pc; + sp = fp; - SET_PC (pc) + ct = *sp++ >> 12; + frame_count--; + fp = stack + 1 + *sp++; + pc = *sp++; + pc = ((long)*sp++ << 9) | pc; - /* Handle resulting value */ - - if (ct == 0) - store (value); - if (ct == 2) - *--sp = value; - - /* Stop main loop for direct calls */ - - if (ct == 2) - finished++; - -}/* ret */ + SET_PC(pc) + /* Handle resulting value */ + if (ct == 0) + store(value); + if (ct == 2) + *--sp = value; + /* Stop main loop for direct calls */ + if (ct == 2) + finished++; +} /* ret */ /* * branch @@ -448,44 +408,38 @@ void ret (zword value) * return true. * */ -void branch (bool flag) +void branch(bool flag) { - long pc; - zword offset; - zbyte specifier; - zbyte off1; - zbyte off2; - - CODE_BYTE (specifier) - - off1 = specifier & 0x3f; - - if (!flag) - specifier ^= 0x80; - - if (!(specifier & 0x40)) { /* it's a long branch */ - - if (off1 & 0x20) /* propagate sign bit */ - off1 |= 0xc0; - - CODE_BYTE (off2) - - offset = (off1 << 8) | off2; - - } else offset = off1; /* it's a short branch */ - - if (specifier & 0x80) { - - if (offset > 1) { /* normal branch */ - - GET_PC (pc) - pc += (short) offset - 2; - SET_PC (pc) - - } else ret (offset); /* special case, return 0 or 1 */ - } - -}/* branch */ + long pc; + zword offset; + zbyte specifier; + zbyte off1; + zbyte off2; + + CODE_BYTE(specifier) + off1 = specifier & 0x3f; + + if (!flag) + specifier ^= 0x80; + + if (!(specifier & 0x40)) { /* it's a long branch */ + if (off1 & 0x20) /* propagate sign bit */ + off1 |= 0xc0; + + CODE_BYTE(off2) + offset = (off1 << 8) | off2; + } else + offset = off1; /* it's a short branch */ + + if (specifier & 0x80) { + if (offset > 1) { /* normal branch */ + GET_PC(pc) + pc += (short)offset - 2; + SET_PC(pc) + } else + ret(offset); /* special case, return 0 or 1 */ + } +} /* branch */ /* @@ -494,22 +448,20 @@ void branch (bool flag) * Store an operand, either as a variable or pushed on the stack. * */ -void store (zword value) +void store(zword value) { - zbyte variable; - - CODE_BYTE (variable) - - if (variable == 0) - *--sp = value; - else if (variable < 16) - *(fp - variable) = value; - else { - zword addr = h_globals + 2 * (variable - 16); - SET_WORD (addr, value) - } + zbyte variable; -}/* store */ + CODE_BYTE(variable) + if (variable == 0) + *--sp = value; + else if (variable < 16) + *(fp - variable) = value; + else { + zword addr = h_globals + 2 * (variable - 16); + SET_WORD(addr, value) + } +} /* store */ /* @@ -524,40 +476,32 @@ void store (zword value) * The interpreter returns the result value on the stack. * */ -int direct_call (zword addr) +int direct_call(zword addr) { - zword saved_zargs[8]; - int saved_zargc; - int i; - - /* Calls to address 0 return false */ - - if (addr == 0) - return 0; - - /* Save operands and operand count */ + zword saved_zargs[8]; + int saved_zargc; + int i; - for (i = 0; i < 8; i++) - saved_zargs[i] = zargs[i]; + /* Calls to address 0 return false */ + if (addr == 0) + return 0; - saved_zargc = zargc; + /* Save operands and operand count */ + for (i = 0; i < 8; i++) + saved_zargs[i] = zargs[i]; + saved_zargc = zargc; - /* Call routine directly */ + /* Call routine directly */ + call(addr, 0, 0, 2); - call (addr, 0, 0, 2); + /* Restore operands and operand count */ + for (i = 0; i < 8; i++) + zargs[i] = saved_zargs[i]; + zargc = saved_zargc; - /* Restore operands and operand count */ - - for (i = 0; i < 8; i++) - zargs[i] = saved_zargs[i]; - - zargc = saved_zargc; - - /* Resulting value lies on top of the stack */ - - return (short) *sp++; - -}/* direct_call */ + /* Resulting value lies on top of the stack */ + return (short)*sp++; +} /* direct_call */ /* @@ -566,20 +510,19 @@ int direct_call (zword addr) * Load and execute an extended opcode. * */ -static void __extended__ (void) +static void __extended__(void) { - zbyte opcode; - zbyte specifier; - - CODE_BYTE (opcode) - CODE_BYTE (specifier) - - load_all_operands (specifier); + zbyte opcode; + zbyte specifier; - if (opcode < 0x1d) /* extended opcodes from 0x1d on */ - ext_opcodes[opcode] (); /* are reserved for future spec' */ + CODE_BYTE(opcode) + CODE_BYTE(specifier) + load_all_operands(specifier); -}/* __extended__ */ + /* extended opcodes from 0x1d on */ + if (opcode < 0x1d) + ext_opcodes[opcode] (); /* are reserved for future spec' */ +} /* __extended__ */ /* @@ -588,11 +531,10 @@ static void __extended__ (void) * Exit game because an unknown opcode has been hit. * */ -static void __illegal__ (void) +static void __illegal__(void) { - runtime_error (ERR_ILL_OPCODE); - -}/* __illegal__ */ + runtime_error(ERR_ILL_OPCODE); +} /* __illegal__ */ /* @@ -601,11 +543,10 @@ static void __illegal__ (void) * no zargs used * */ -void z_catch (void) +void z_catch(void) { - store (frame_count); - -}/* z_catch */ + store(frame_count); +} /* z_catch */ /* @@ -615,18 +556,17 @@ void z_catch (void) * zargs[1] = stack frame * */ -void z_throw (void) +void z_throw(void) { - if (zargs[1] > frame_count) - runtime_error (ERR_BAD_FRAME); - - /* Unwind the stack a frame at a time. */ - for (; frame_count > zargs[1]; --frame_count) - fp = stack + 1 + fp[1]; + if (zargs[1] > frame_count) + runtime_error(ERR_BAD_FRAME); - ret (zargs[0]); + /* Unwind the stack a frame at a time. */ + for (; frame_count > zargs[1]; --frame_count) + fp = stack + 1 + fp[1]; -}/* z_throw */ + ret(zargs[0]); +} /* z_throw */ /* @@ -638,12 +578,11 @@ void z_throw (void) * zargs[7] = seventh argument (optional) * */ -void z_call_n (void) +void z_call_n(void) { - if (zargs[0] != 0) - call (zargs[0], zargc - 1, zargs + 1, 1); - -}/* z_call_n */ + if (zargs[0] != 0) + call(zargs[0], zargc - 1, zargs + 1, 1); +} /* z_call_n */ /* @@ -655,14 +594,13 @@ void z_call_n (void) * zargs[7] = seventh argument (optional) * */ -void z_call_s (void) +void z_call_s(void) { - if (zargs[0] != 0) - call (zargs[0], zargc - 1, zargs + 1, 0); - else - store (0); - -}/* z_call_s */ + if (zargs[0] != 0) + call(zargs[0], zargc - 1, zargs + 1, 0); + else + store(0); +} /* z_call_s */ /* @@ -671,14 +609,14 @@ void z_call_s (void) * zargs[0] = number of arguments * */ -void z_check_arg_count (void) +void z_check_arg_count(void) { - if (fp == stack + STACK_SIZE) - branch (zargs[0] == 0); - else - branch (zargs[0] <= (*fp & 0xff)); + if (fp == stack + STACK_SIZE) + branch(zargs[0] == 0); + else + branch(zargs[0] <= (*fp & 0xff)); -}/* z_check_arg_count */ +} /* z_check_arg_count */ /* @@ -687,20 +625,18 @@ void z_check_arg_count (void) * zargs[0] = PC relative address * */ -void z_jump (void) +void z_jump(void) { - long pc; - - GET_PC (pc) + long pc; - pc += (short) zargs[0] - 2; + GET_PC(pc) + pc += (short)zargs[0] - 2; - if (pc >= story_size) - runtime_error (ERR_ILL_JUMP_ADDR); + if (pc >= story_size) + runtime_error(ERR_ILL_JUMP_ADDR); - SET_PC (pc) - -}/* z_jump */ + SET_PC(pc) +} /* z_jump */ /* @@ -709,11 +645,10 @@ void z_jump (void) * no zargs used * */ -void z_nop (void) +void z_nop(void) { - /* Do nothing */ - -}/* z_nop */ + /* Do nothing */ +} /* z_nop */ /* @@ -722,11 +657,10 @@ void z_nop (void) * no zargs used * */ -void z_quit (void) +void z_quit(void) { - finished = 9999; - -}/* z_quit */ + finished = 9999; +} /* z_quit */ /* @@ -735,11 +669,10 @@ void z_quit (void) * zargs[0] = value to return * */ -void z_ret (void) +void z_ret(void) { - ret (zargs[0]); - -}/* z_ret */ + ret(zargs[0]); +} /* z_ret */ /* @@ -748,11 +681,10 @@ void z_ret (void) * no zargs used * */ -void z_ret_popped (void) +void z_ret_popped(void) { - ret (*sp++); - -}/* z_ret_popped */ + ret(*sp++); +} /* z_ret_popped */ /* @@ -761,11 +693,10 @@ void z_ret_popped (void) * no zargs used * */ -void z_rfalse (void) +void z_rfalse(void) { - ret (0); - -}/* z_rfalse */ + ret(0); +} /* z_rfalse */ /* @@ -774,8 +705,7 @@ void z_rfalse (void) * no zargs used * */ -void z_rtrue (void) +void z_rtrue(void) { - ret (1); - -}/* z_rtrue */ + ret(1); +} /* z_rtrue */ -- 2.34.1