void mode_extension( TEXT_RLD *tr, ADDR_MODE *mode, STREAM *str) { EX_TREE *value = mode->offset; SYMBOL *sym; unsigned offset; /* Also frees the mode. */ if (value == NULL) { free_addr_mode(mode); return; } if (value->type == EX_LIT) { if (mode->rel) /* PC-relative? */ store_displaced_word(str, tr, 2, value->data.lit); else store_word(str, tr, 2, value->data.lit); /* Just a known value. */ } else if (express_sym_offset(value, &sym, &offset)) { if ((sym->flags & (SYMBOLFLAG_GLOBAL | SYMBOLFLAG_DEFINITION)) == SYMBOLFLAG_GLOBAL) { /* Reference to a global symbol. */ /* Global symbol plus offset */ if (mode->rel) store_global_displaced_offset_word(str, tr, 2, offset, sym->label); else store_global_offset_word(str, tr, 2, offset, sym->label); } else { /* Relative to non-external symbol. */ if (current_pc->section == sym->section) { /* In the same section */ if (mode->rel) { /* I can compute this myself. */ store_word(str, tr, 2, sym->value + offset - DOT - 2); } else store_internal_word(str, tr, 2, sym->value + offset); } else { /* In a different section */ if (mode->rel) store_psect_displaced_offset_word(str, tr, 2, sym->value + offset, sym->section->label); else store_psect_offset_word(str, tr, 2, sym->value + offset, sym->section->label); } } } else { /* Complex relocation */ if (mode->rel) store_complex_displaced(str, tr, 2, mode->offset); else store_complex(str, tr, 2, mode->offset); } free_addr_mode(mode); }
void check_storeload(){ create_segment(1, 0x1); create_segment(5, 0x2); create_segment(10, 0x3); store_word(0x1, 0, 0x1); store_word(0x2, 4, 0x10); store_word(0x3, 4, 0x100); uint64_t a = load_word(1, 0); uint64_t b = load_word(2, 4); uint64_t c = load_word(3, 4); assert(a == 0x1 && b == 0x10 && c == 0x100); }
void store_value( STACK *stack, TEXT_RLD *tr, int size, EX_TREE *value) { SYMBOL *sym; unsigned offset; implicit_gbl(value); /* turn undefined symbols into globals */ if (value->type == EX_LIT) { store_word(stack->top, tr, size, value->data.lit); } else if (!express_sym_offset(value, &sym, &offset)) { store_complex(stack->top, tr, size, value); } else { if ((sym->flags & (SYMBOLFLAG_GLOBAL | SYMBOLFLAG_DEFINITION)) == SYMBOLFLAG_GLOBAL) { store_global_offset_word(stack->top, tr, size, sym->value + offset, sym->label); } else if (sym->section != current_pc->section) { store_psect_offset_word(stack->top, tr, size, sym->value + offset, sym->section->label); } else { store_internal_word(stack->top, tr, size, sym->value + offset); } } }
/** * Copy @size bytes to the buffer @dest_tracer from the address * @src_tracee within the memory space of the @tracee process. It * returns -errno if an error occured, otherwise 0. */ int read_data(const Tracee *tracee, void *dest_tracer, word_t src_tracee, word_t size) { word_t *src = (word_t *)src_tracee; word_t *dest = (word_t *)dest_tracer; word_t nb_trailing_bytes; word_t nb_full_words; word_t word, i, j; uint8_t *last_src_word; uint8_t *last_dest_word; #if defined(HAVE_PROCESS_VM) long status; struct iovec local; struct iovec remote; local.iov_base = dest; local.iov_len = size; remote.iov_base = src; remote.iov_len = size; status = process_vm_readv(tracee->pid, &local, 1, &remote, 1, 0); if (status == size) return 0; /* Fallback to ptrace if something went wrong. */ #endif /* HAVE_PROCESS_VM */ nb_trailing_bytes = size % sizeof(word_t); nb_full_words = (size - nb_trailing_bytes) / sizeof(word_t); /* Copy one word by one word, except for the last one. */ for (i = 0; i < nb_full_words; i++) { word = ptrace(PTRACE_PEEKDATA, tracee->pid, src + i, NULL); if (errno != 0) { notice(tracee, WARNING, SYSTEM, "ptrace(PEEKDATA)"); return -EFAULT; } store_word(&dest[i], word); } /* Copy the bytes from the last word carefully since we have * to not overwrite the bytes lying beyond @dest_tracer. */ word = ptrace(PTRACE_PEEKDATA, tracee->pid, src + i, NULL); if (errno != 0) { notice(tracee, WARNING, SYSTEM, "ptrace(PEEKDATA)"); return -EFAULT; } last_dest_word = (uint8_t *)&dest[i]; last_src_word = (uint8_t *)&word; for (j = 0; j < nb_trailing_bytes; j++) last_dest_word[j] = last_src_word[j]; return 0; }
void FileAccess::store_dword(Uint32 p_dest) { ERR_FAIL_COND(!f); ERR_FAIL_COND( !(current_mode&WRITE ) ); Uint16 aux_word1,aux_word2; aux_word1=p_dest & 0xFFFF; aux_word2=p_dest>>16; // endian_swap(aux_word1,aux_word2); store_word(aux_word1); store_word(aux_word2); }
/** * Tests 'store_word()' and 'get_word()'. */ void test_store_get_word() { allocate(); uint16_t dummy_value = 0xFF00; store_word(ram, DUMMY_OFFSET, dummy_value); CU_ASSERT(dummy_value == get_word(ram, DUMMY_OFFSET)); deallocate(); }
static LPUB8 get_TT_glyph(fapi_ufst_server *r, FAPI_font *ff, UW16 chId) { pcleo_glyph_list_elem *g; PCLETTO_CHDR *h; ufst_common_font_data *d = (ufst_common_font_data *)r->fc.font_hdr - 1; LPUB8 q; ushort glyph_length = ff->get_glyph(ff, chId, 0, 0); bool use_XL_format = ff->is_mtx_skipped; /* * The client must set ff->is_mtx_skipped iff * it requests a replaced lsb for True Type. * If it is set, a replaced width to be supplied. * This constraint is derived from UFST restriction : * the font header format must be compatible with * glyph header format. */ if (glyph_length == (ushort)-1) { r->callback_error = e_invalidfont; return 0; } g = (pcleo_glyph_list_elem *)r->client_mem.alloc(&r->client_mem, sizeof(pcleo_glyph_list_elem) + (use_XL_format ? 12 : sizeof(PCLETTO_CHDR)) + glyph_length + 2, "PCLETTO char"); if (g == 0) { r->callback_error = e_VMerror; return 0; } g->chId = chId; g->next = d->glyphs; d->glyphs = g; h = (PCLETTO_CHDR *)(g + 1); h->h.format = 15; if (use_XL_format) { h->h.continuation = 2; q = (LPUB8)h + 2; store_word(&q, (ushort)(glyph_length + 10)); store_word(&q, (ushort)(r->sb_x >> r->If.frac_shift)); /* see can_replace_metrics */ store_word(&q, (ushort)(r->aw_x >> r->If.frac_shift)); store_word(&q, 0); store_word(&q, chId); } else {
int do_word( STACK *stack, TEXT_RLD *tr, char *cp, int size) { int comma; if (size == 2 && (DOT & 1)) { report(stack->top, ".WORD on odd boundary\n"); store_word(stack->top, tr, 1, 0); /* Align it */ } cp = skipwhite(cp); do { if (cp[0] == ',') { /* Empty expressions count as 0 */ store_word(stack->top, tr, size, 0); } else { EX_TREE *value = parse_expr(cp, 0); if (value->cp > cp) { store_value(stack, tr, size, value); cp = value->cp; } else { report(stack->top, "Invalid expression in .WORD\n"); cp = ""; /* force loop to end */ } free_tree(value); } } while (cp = skipdelim_comma(cp, &comma), !EOL(*cp)); if (comma) { /* Trailing empty expressions count as 0 */ store_word(stack->top, tr, size, 0); } return 1; }
void opcode_push_user_stack(void) { uint16_t spare_slots; uint8_t *user_stack; TRACE_LOG("Opcode: PUSH_USER_STACK.\n"); (void)read_z_result_variable(); user_stack = z_mem + (uint16_t)op[1]; if ((spare_slots = load_word(user_stack)) > 0) { store_word(user_stack + spare_slots, (uint16_t)op[0]); spare_slots--; store_word(user_stack, spare_slots); evaluate_branch((uint8_t)1); } else { evaluate_branch((uint8_t)0); } }
void set_variable(uint8_t variable_number, uint16_t data, bool keep_stack_index) { if (variable_number == 0) { if (stack_words_from_active_routine == MAXIMUM_STACK_ENTRIES_PER_ROUTINE) i18n_translate_and_exit( libfizmo_module_name, i18n_libfizmo_MAXIMUM_NUMBER_OF_STACK_ENTRIES_PER_ROUTINE_P0D_EXCEEDED, -1, (long int)MAXIMUM_STACK_ENTRIES_PER_ROUTINE); if (bool_equal(keep_stack_index, true)) { z_stack_pull_word(); z_stack_push_word(data); } else { z_stack_push_word(data); stack_words_from_active_routine++; } } else if (variable_number < 0x10) { if (variable_number > number_of_locals_active) i18n_translate_and_exit( libfizmo_module_name, i18n_libfizmo_TRYING_TO_STORE_VARIABLE_L_P0D_BUT_ONLY_P1D_VARIABLES_ACTIVE, -1, (long int)variable_number-1, (long int)number_of_locals_active); variable_number--; TRACE_LOG("Storing %x in L0%x.\n", data, variable_number); local_variable_storage_index[variable_number] = data; } else { variable_number -= 0x10; TRACE_LOG("Setting global variable G%02x to %x.\n", variable_number, data); store_word( /*@-nullderef@*/ active_z_story->global_variables /*@-nullderef@*/ +(variable_number*2), data); } }
void opcode_storew(void) { uint8_t *address = z_mem + (uint16_t)(op[0] + ((int16_t)op[1])*2); TRACE_LOG("Opcode: STOREW.\n"); if (address > active_z_story->dynamic_memory_end) { TRACE_LOG("Trying to storew to %x which is above dynamic memory.\n", address); } else { TRACE_LOG("Storing %x to %x.\n", op[2], address); store_word(z_mem + (uint16_t)(op[0] + ((int16_t)op[1])*2), op[2]); } }
void opcode_pull(void) { uint16_t value = 0; uint16_t spare_slots; uint8_t *user_stack; TRACE_LOG("Opcode: PULL.\n"); if (ver == 6) (void)read_z_result_variable(); if ( (ver != 6) || (number_of_operands < 1) ) { if ( (stack_words_from_active_routine == 0) && (bool_equal(skip_active_routines_stack_check_warning, false)) ) { i18n_translate_and_exit( libfizmo_module_name, i18n_libfizmo_NOT_ENOUGH_STACK_WORDS_FROM_LOCAL_ROUTINE_ON_STACK, -1); } else { TRACE_LOG("Pulling to variable %x.\n", op[0]); value = z_stack_pull_word(); stack_words_from_active_routine--; } } else { user_stack = z_mem + (uint16_t)op[0]; spare_slots = load_word(user_stack); spare_slots++; value = load_word(user_stack + spare_slots); store_word(user_stack, spare_slots); } if (ver == 6) set_variable(z_res_var, value, true); else set_variable(op[0], value, true); }
void program_interrupt (SIM_DESC sd, sim_cpu *cpu, sim_cia cia, SIM_SIGNAL sig) { int status; struct hw *device; static int in_interrupt = 0; #ifdef SIM_CPU_EXCEPTION_TRIGGER SIM_CPU_EXCEPTION_TRIGGER(sd,cpu,cia); #endif /* avoid infinite recursion */ if (in_interrupt) { (*mn10300_callback->printf_filtered) (mn10300_callback, "ERROR: recursion in program_interrupt during software exception dispatch."); } else { in_interrupt = 1; /* copy NMI handler code from dv-mn103cpu.c */ store_word (SP - 4, CPU_PC_GET (cpu)); store_half (SP - 8, PSW); /* Set the SYSEF flag in NMICR by backdoor method. See dv-mn103int.c:write_icr(). This is necessary because software exceptions are not modelled by actually talking to the interrupt controller, so it cannot set its own SYSEF flag. */ if ((NULL != board) && (strcmp(board, BOARD_AM32) == 0)) store_byte (0x34000103, 0x04); } PSW &= ~PSW_IE; SP = SP - 8; CPU_PC_SET (cpu, 0x40000008); in_interrupt = 0; sim_engine_halt(sd, cpu, NULL, cia, sim_stopped, sig); }
/* Input: a file pointer * Output: a Mem_T structure * Purpose: Initializes the Mem_T memory structure. Stores 32 bit words from * input into segment 0 in memory. This is location which holds the * instructions that the um will execute */ Mem_T Mem_new(FILE *input) { long unsigned lSize; uint32_t *buffer; size_t buffer_size; assert(input != NULL); Mem_T memory = malloc(sizeof(*memory)); assert(memory!=NULL); memory->segment_seq = Seq_new(0); memory->reusable_indices = Seq_new(0); /* determines length of input file */ fseek(input, 0, SEEK_END); lSize = ftell(input); rewind(input); /* creates buffer that will grab words from input*/ buffer = malloc(sizeof(uint32_t) * lSize/4); assert(buffer != NULL); buffer_size = fread(buffer, 4, lSize / 4, input); if(buffer_size != lSize/4){ fprintf(stderr, "Reading error\n"); } /* Program index will be 0*/ int programIndex = map_segment(memory, buffer_size); /*transfers instructions from buffer to segment 0 in memory*/ for(unsigned i = 0; i < 3; i++){ uint32_t word = buffer[i]; word = flip_word(word); fprintf(stderr, "word: %x\n", word); store_word(memory, word, programIndex, i); } free(buffer); return memory; }
static void store_complex( STREAM *refstr, TEXT_RLD *tr, int size, EX_TREE *value) { TEXT_COMPLEX tx; change_dot(tr, size); /* About to store - update DOT */ implicit_gbl(value); /* Turn undefined symbols into globals */ text_complex_begin(&tx); /* Open complex expression */ if (!complex_tree(&tx, value)) { /* Translate */ report(refstr, "Invalid expression\n"); store_word(refstr, tr, size, 0); } else { list_word(refstr, DOT, 0, size, "C"); text_complex_commit(tr, &DOT, size, &tx, 0); } }
static void store_complex_displaced( STREAM *refstr, TEXT_RLD *tr, int size, EX_TREE *value) { TEXT_COMPLEX tx; change_dot(tr, size); implicit_gbl(value); /* Turn undefined symbols into globals */ text_complex_begin(&tx); if (!complex_tree(&tx, value)) { report(refstr, "Invalid expression\n"); store_word(refstr, tr, size, 0); } else { list_word(refstr, DOT, 0, size, "C"); text_complex_commit_displaced(tr, &DOT, size, &tx, 0); } }
static int store_word_list(struct rs_dll *list, FILE *f) { int ret; char *cret; char buf[0x100]; char *word; rs_dll_init(list, NULL); while ((cret = fgets(buf, 0xFF, f)) != NULL) { buf[0xFF] = '\0'; word = ut_string_strip(buf); if (strlen(word) != 0) { ret = store_word(list, word); if (ret < 0) { ULOGE("store_word: %s", strerror(-ret)); return ret; } } } return 0; }
/** * A utility function to avoid duplication when testing push() implementations. * * @param pop_value TODO * @param push_mode TODO * @param storage_location TODO * @param source_location TODO */ void push_test_util(uint32_t pop_value, uint8_t push_mode, uint32_t storage_location, int source_location) { allocate(); if (storage_location != IMMEDIATE) { if (storage_location == NEXT2_INSTR) store_dword(ram, NEXT_INSTR, NEXT2_INSTR); if (source_location == SOURCE_IMMEDIATE && storage_location == NEXT3_INSTR) { store_dword(ram, NEXT_INSTR, NEXT2_INSTR); store_dword(ram, NEXT2_INSTR, NEXT3_INSTR); } else if (source_location == SOURCE_REGISTER) r[TEST_REGISTER] = NEXT_INSTR; } switch (pop_value) { case DUMMY_VALUE_8: if (source_location == SOURCE_IMMEDIATE) { store_byte(ram, storage_location, DUMMY_VALUE_8); exec(create_instruction(INSTR_PUSH, push_mode, EMPTY_BYTE, DUMMY_VALUE_8)); } else if (source_location == SOURCE_REGISTER) { if (storage_location == IMMEDIATE) r[TEST_REGISTER] = DUMMY_VALUE_8; else store_byte(ram, storage_location, DUMMY_VALUE_8); } break; case DUMMY_VALUE_16: if (source_location == SOURCE_IMMEDIATE) { store_word(ram, storage_location, DUMMY_VALUE_16); exec(create_instruction(INSTR_PUSH, push_mode, DUMMY_VALUE_8, DUMMY_VALUE_8)); } else if (source_location == SOURCE_REGISTER) { if (storage_location == IMMEDIATE) r[TEST_REGISTER] = DUMMY_VALUE_16; else store_word(ram, storage_location, DUMMY_VALUE_16); } break; case DUMMY_VALUE_32: if (source_location == SOURCE_IMMEDIATE) { store_dword(ram, storage_location, DUMMY_VALUE_32); exec(create_instruction(INSTR_PUSH, push_mode, EMPTY_BYTE, EMPTY_BYTE)); } else if (source_location == SOURCE_REGISTER) { if (storage_location == IMMEDIATE) r[TEST_REGISTER] = DUMMY_VALUE_32; else store_dword(ram, storage_location, DUMMY_VALUE_32); } break; default: CU_FAIL("Invalid pop_value"); } if (source_location == SOURCE_REGISTER) exec(create_instruction(INSTR_PUSH, push_mode, EMPTY_BYTE, TEST_REGISTER)); CU_ASSERT(pop_value == pop()); deallocate(); }
static void deliver_mn103cpu_interrupt (struct hw *me, void *data) { struct mn103cpu *controller = hw_data (me); SIM_DESC simulator = hw_system (me); sim_cpu *cpu = STATE_CPU (simulator, 0); if (controller->pending_reset) { controller->pending_reset = 0; /* need to clear all registers et.al! */ HW_TRACE ((me, "Reset!")); hw_abort (me, "Reset!"); } else if (controller->pending_nmi) { controller->pending_nmi = 0; store_word (SP - 4, CPU_PC_GET (cpu)); store_half (SP - 8, PSW); PSW &= ~PSW_IE; SP = SP - 8; CPU_PC_SET (cpu, 0x40000008); HW_TRACE ((me, "nmi pc=0x%08lx psw=0x%04x sp=0x%08lx", (long) CPU_PC_GET (cpu), (unsigned) PSW, (long) SP)); } else if ((controller->pending_level < EXTRACT_PSW_LM) && (PSW & PSW_IE)) { /* Don't clear pending level. Request continues to be pending until the interrupt controller clears/changes it */ store_word (SP - 4, CPU_PC_GET (cpu)); store_half (SP - 8, PSW); PSW &= ~PSW_IE; PSW &= ~PSW_LM; PSW |= INSERT_PSW_LM (controller->pending_level); SP = SP - 8; CPU_PC_SET (cpu, 0x40000000 + controller->interrupt_vector[controller->pending_level]); HW_TRACE ((me, "port-out ack %d", controller->pending_level)); hw_port_event (me, ACK_PORT, controller->pending_level); HW_TRACE ((me, "int level=%d pc=0x%08lx psw=0x%04x sp=0x%08lx", controller->pending_level, (long) CPU_PC_GET (cpu), (unsigned) PSW, (long) SP)); } if (controller->pending_level < 7) /* FIXME */ { /* As long as there is the potential need to deliver an interrupt we keep rescheduling this routine. */ if (controller->pending_handler != NULL) controller->pending_handler = hw_event_queue_schedule (me, 1, deliver_mn103cpu_interrupt, NULL); } else { /* Don't bother re-scheduling the interrupt handler as there is nothing to deliver */ controller->pending_handler = NULL; } }
void poll_inputs() { short changed = 0; short button_state = 0; short i; short *pOldValue = old_sensor_values; sensor_t *pSensor = &sensors[0]; char packet_available; throttle_count--; if( throttle_count == 0){ throttle_count = throttle; // If we're not polling or someone already has the monitor // return. if (!poller || get_monitor_count((&(poller->_super))) != 0) return; // We do not have a thread that we can use to grab // the monitor but that's OK because we are atomic // anyway. // Check the sensor canonical values. for (i = 1<<SENSOR_POS; i<(1<<BUTTON_POS); i <<= 1, pOldValue++, pSensor++) { if (*pOldValue != pSensor->value) { changed |= i; *pOldValue = pSensor->value; } } // Check the button status read_buttons (0x3000, &button_state); button_state <<= BUTTON_POS; // Shift into poll position changed |= button_state ^ old_button_state; old_button_state = button_state; // Check serial status check_for_data ( &packet_available, null); if (packet_available) { changed |= 1 << SERIAL_RECEIVED_POS; } // Only wake threads up if things have changed since // we last looked. if (changed) { // Or in the latest changes. Some threads may not have // responded to earlier changes yet so we can't // just overwrite them. short jword = 0; store_word((byte*)(&jword), 2, changed); poller->changed |= jword; #if DEBUG_POLL jword = get_word((byte*)&poller->changed, 2); printf("Poller: poller->changed = 0x%1X\n", jword); #endif // poller.notifyAll() monitor_notify_unchecked(&poller->_super, 1); } } }
/** * Copy to @dest_tracer at most @max_size bytes from the string * pointed to by @src_tracee within the memory space of the @tracee * process. This function returns -errno on error, otherwise * it returns the number in bytes of the string, including the * end-of-string terminator. */ int read_string(const Tracee *tracee, char *dest_tracer, word_t src_tracee, word_t max_size) { word_t *src = (word_t *)src_tracee; word_t *dest = (word_t *)dest_tracer; word_t nb_trailing_bytes; word_t nb_full_words; word_t word, i, j; uint8_t *src_word; uint8_t *dest_word; if (belongs_to_heap_prealloc(tracee, src_tracee)) return -EFAULT; #if defined(HAVE_PROCESS_VM) /* [process_vm] system calls do not check the memory regions * in the remote process until just before doing the * read/write. Consequently, a partial read/write [1] may * result if one of the remote_iov elements points to an * invalid memory region in the remote process. No further * reads/writes will be attempted beyond that point. Keep * this in mind when attempting to read data of unknown length * (such as C strings that are null-terminated) from a remote * process, by avoiding spanning memory pages (typically 4KiB) * in a single remote iovec element. (Instead, split the * remote read into two remote_iov elements and have them * merge back into a single write local_iov entry. The first * read entry goes up to the page boundary, while the second * starts on the next page boundary.). * * [1] Partial transfers apply at the granularity of iovec * elements. These system calls won't perform a partial * transfer that splits a single iovec element. * * -- man 2 process_vm_readv */ long status; size_t size; size_t offset; struct iovec local; struct iovec remote; static size_t chunk_size = 0; static uintptr_t chunk_mask; /* A chunk shall not cross a page boundary. */ if (chunk_size == 0) { chunk_size = sysconf(_SC_PAGESIZE); chunk_size = (chunk_size > 0 && chunk_size < 1024 ? chunk_size : 1024); chunk_mask = ~(chunk_size - 1); } /* Read the string by chunk. */ offset = 0; do { uintptr_t current_chunk = (src_tracee + offset) & chunk_mask; uintptr_t next_chunk = current_chunk + chunk_size; /* Compute the number of bytes available up to the * next chunk or up to max_size. */ size = next_chunk - (src_tracee + offset); size = (size < max_size - offset ? size : max_size - offset); local.iov_base = (uint8_t *)dest + offset; local.iov_len = size; remote.iov_base = (uint8_t *)src + offset; remote.iov_len = size; status = process_vm_readv(tracee->pid, &local, 1, &remote, 1, 0); if ((size_t) status != size) goto fallback; status = strnlen(local.iov_base, size); if ((size_t) status < size) { size = offset + status + 1; assert(size <= max_size); return size; } offset += size; } while (offset < max_size); assert(offset == max_size); /* Fallback to ptrace if something went wrong. */ fallback: #endif /* HAVE_PROCESS_VM */ nb_trailing_bytes = max_size % sizeof(word_t); nb_full_words = (max_size - nb_trailing_bytes) / sizeof(word_t); /* Copy one word by one word, except for the last one. */ for (i = 0; i < nb_full_words; i++) { word = ptrace(PTRACE_PEEKDATA, tracee->pid, src + i, NULL); if (errno != 0) return -EFAULT; store_word(&dest[i], word); /* Stop once an end-of-string is detected. */ src_word = (uint8_t *)&word; for (j = 0; j < sizeof(word_t); j++) if (src_word[j] == '\0') return i * sizeof(word_t) + j + 1; } /* Copy the bytes from the last word carefully since we have * to not overwrite the bytes lying beyond @dest_tracer. */ word = ptrace(PTRACE_PEEKDATA, tracee->pid, src + i, NULL); if (errno != 0) return -EFAULT; dest_word = (uint8_t *)&dest[i]; src_word = (uint8_t *)&word; for (j = 0; j < nb_trailing_bytes; j++) { dest_word[j] = src_word[j]; if (src_word[j] == '\0') break; } return i * sizeof(word_t) + j + 1; }
void opcode_get_cursor_array(void) { TRACE_LOG("Opcode: GET_CURSOR\n"); store_word(z_mem + op[0] , active_interface->get_cursor_row()); store_word(z_mem + op[0] + 1, active_interface->get_cursor_column()); }
//Executes 'cmd' and increases program counter 'pc'. void execute_instruction(COMMAND cmd, COMMAND* commands_arr, int* registers_arr, byte* mem, int* pc){ /*Instruction Type: R-type*/ if (strcmp(cmd.cmd_type,"R") == 0){ if(stricmp(cmd.cmd_name,"add") == 0){ registers_arr[cmd.Rd] = registers_arr[cmd.Rs]+ registers_arr[cmd.Rt]; (*pc)++; }else if (stricmp(cmd.cmd_name,"sub") == 0){ registers_arr[cmd.Rd] = registers_arr[cmd.Rs] - registers_arr[cmd.Rt]; (*pc)++; }else if (stricmp(cmd.cmd_name,"mul") == 0){ registers_arr[cmd.Rd] = registers_arr[cmd.Rs]*registers_arr[cmd.Rt]; (*pc)++; }else if (stricmp(cmd.cmd_name,"div") == 0){ registers_arr[cmd.Rd] = registers_arr[cmd.Rs]/registers_arr[cmd.Rt]; (*pc)++; }else if (stricmp(cmd.cmd_name,"slt") == 0){ if (registers_arr[cmd.Rs] < registers_arr[cmd.Rt]){ registers_arr[cmd.Rd] = 1; }else{ registers_arr[cmd.Rd] = 0; } (*pc)++; } }else if (strcmp(cmd.cmd_type,"I") == 0){/*Instruction Type: I-type*/ if (stricmp(cmd.cmd_name,"addi") == 0){ registers_arr[cmd.Rt] = registers_arr[cmd.Rs] + atoi(cmd.immediate); (*pc)++; }else if (stricmp(cmd.cmd_name,"subi") == 0){ registers_arr[cmd.Rt]= registers_arr[cmd.Rs] - atoi(cmd.immediate); (*pc)++; }else if (stricmp(cmd.cmd_name,"lw") == 0){ int address = registers_arr[cmd.Rs] + atoi(cmd.immediate); registers_arr[cmd.Rt] = load_word(address, mem); (*pc)++; }else if (stricmp(cmd.cmd_name,"sw") == 0){ int address = registers_arr[cmd.Rs] + atoi(cmd.immediate); store_word (registers_arr[cmd.Rt], address, mem); (*pc)++; }else if (stricmp(cmd.cmd_name,"slti") == 0){ if (registers_arr[cmd.Rs] < atoi(cmd.immediate)){ registers_arr[cmd.Rt] = 1; }else{ registers_arr[cmd.Rt] = 0; } (*pc)++; }else if (stricmp(cmd.cmd_name,"beq") == 0){ if (cmd.Rs == cmd.Rt){ for(int i = 0; strcmp(commands_arr[i].cmd_type, "H") != 0; i++){ if (commands_arr[i].b_is_labled){ if (strcmp(cmd.immediate , commands_arr[i].label) == 0){ *pc = i; } }else{ continue; } } }else{ (*pc)++; } }else if (stricmp(cmd.cmd_name,"bne") == 0){ if (registers_arr[cmd.Rs] != registers_arr[cmd.Rt]){ for(int i = 0; strcmp(commands_arr[i].cmd_type, "H") != 0; i++){ if (commands_arr[i].b_is_labled){ if (strcmp(cmd.immediate , commands_arr[i].label) == 0){ *pc = i; break; } }else{ continue; } } }else{ (*pc)++; } } }else{/*Instruction Type: J-type*/ if (stricmp(cmd.cmd_name,"j") == 0){ for(int i = 0; strcmp(commands_arr[i].cmd_type, "H") != 0; i++){ if (commands_arr[i].b_is_labled){ if (strcmp(cmd.address_label , commands_arr[i].label) == 0){ (*pc = i); break; } }else{ continue; } } }else if (stricmp(cmd.cmd_name,"jr") == 0){ //NO NEED TO IMPLEMENT FOR NOW } } if (registers_arr[0] !=0){// make sure that $R0=0 registers_arr[0] = 0; } }