📄 mem.c
字号:
/* SPIM S20 MIPS simulator. Code to create, maintain and access memory. Copyright (C) 1990-2004 by James Larus (larus@cs.wisc.edu). ALL RIGHTS RESERVED. SPIM is distributed under the following conditions: You may make copies of SPIM for your own use and modify those copies. All copies of SPIM must retain my name and copyright notice. You may not sell SPIM or distributed SPIM in conjunction with a commerical product or service without the expressed written consent of James Larus. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. *//* $Header: /Software/SPIM/src/mem.c 28 11/27/04 8:55p Larus $*/#include "spim.h"#include "string-stream.h"#include "spim-utils.h"#include "inst.h"#include "reg.h"#include "mem.h"/* Exported Variables: */reg_word R[R_LENGTH];reg_word HI, LO;int HI_present, LO_present;mem_addr PC, nPC;double *FPR; /* Dynamically allocate so overlay */float *FGR; /* is possible */int *FWR; /* is possible */reg_word CCR[4][32], CPR[4][32];instruction **text_seg;int text_modified; /* Non-zero means text segment was written */mem_addr text_top;mem_word *data_seg;int data_modified; /* Non-zero means a data segment was written */short *data_seg_h; /* Points to same vector as DATA_SEG */BYTE_TYPE *data_seg_b; /* Ditto */mem_addr data_top;mem_addr gp_midpoint; /* Middle of $gp area */mem_word *stack_seg;short *stack_seg_h; /* Points to same vector as STACK_SEG */BYTE_TYPE *stack_seg_b; /* Ditto */mem_addr stack_bot;instruction **k_text_seg;mem_addr k_text_top;mem_word *k_data_seg;short *k_data_seg_h;BYTE_TYPE *k_data_seg_b;mem_addr k_data_top;/* Local functions: */static mem_word bad_mem_read (mem_addr addr, int mask);static void bad_mem_write (mem_addr addr, mem_word value, int mask);static instruction *bad_text_read (mem_addr addr);static void bad_text_write (mem_addr addr, instruction *inst);static void free_instructions (instruction **inst, int n);static mem_word read_memory_mapped_IO (mem_addr addr);static void write_memory_mapped_IO (mem_addr addr, mem_word value);/* Local variables: */static int32 data_size_limit, stack_size_limit, k_data_size_limit;/* Memory is allocated in five chunks: text, data, stack, kernel text, and kernel data. The arrays are independent and have different semantics. text is allocated from 0x400000 up and only contains INSTRUCTIONs. It does not expand. data is allocated from 0x10000000 up. It can be extended by the SBRK system call. Programs can only read and write this segment. stack grows from 0x7fffefff down. It is automatically extended. Programs can only read and write this segment. k_text is like text, except its is allocated from 0x80000000 up. k_data is like data, but is allocated from 0x90000000 up. Both kernel text and kernel data can only be accessed in kernel mode.*//* The text segments contain pointers to instructions, not actual instructions, so they must be allocated large enough to hold as many pointers as there would be instructions (the two differ on machines in which pointers are not 32 bits long). The following calculations round up in case size is not a multiple of BYTES_PER_WORD. */#define BYTES_TO_INST(N) (((N) + BYTES_PER_WORD - 1) / BYTES_PER_WORD * sizeof(instruction*))voidmake_memory (int text_size, int data_size, int data_limit, int stack_size, int stack_limit, int k_text_size, int k_data_size, int k_data_limit){ if (data_size <= 65536) data_size = 65536; data_size = ROUND_UP(data_size, BYTES_PER_WORD); /* Keep word aligned */ if (text_seg == NULL) text_seg = (instruction **) xmalloc (BYTES_TO_INST(text_size)); else { free_instructions (text_seg, (text_top - TEXT_BOT) / BYTES_PER_WORD); text_seg = (instruction **) realloc (text_seg, BYTES_TO_INST(text_size)); } memclr (text_seg, BYTES_TO_INST(text_size)); text_top = TEXT_BOT + text_size; data_size = ROUND_UP(data_size, BYTES_PER_WORD); /* Keep word aligned */ if (data_seg == NULL) data_seg = (mem_word *) xmalloc (data_size); else data_seg = (mem_word *) realloc (data_seg, data_size); memclr (data_seg, data_size); data_seg_b = (BYTE_TYPE *) data_seg; data_seg_h = (short *) data_seg; data_top = DATA_BOT + data_size; data_size_limit = data_limit; stack_size = ROUND_UP(stack_size, BYTES_PER_WORD); /* Keep word aligned */ if (stack_seg == NULL) stack_seg = (mem_word *) xmalloc (stack_size); else stack_seg = (mem_word *) realloc (stack_seg, stack_size); memclr (stack_seg, stack_size); stack_seg_b = (BYTE_TYPE *) stack_seg; stack_seg_h = (short *) stack_seg; stack_bot = STACK_TOP - stack_size; stack_size_limit = stack_limit; if (k_text_seg == NULL) k_text_seg = (instruction **) xmalloc (BYTES_TO_INST(k_text_size)); else { free_instructions (k_text_seg, (k_text_top - K_TEXT_BOT) / BYTES_PER_WORD); k_text_seg = (instruction **) realloc(k_text_seg, BYTES_TO_INST(k_text_size)); } memclr (k_text_seg, BYTES_TO_INST(k_text_size)); k_text_top = K_TEXT_BOT + k_text_size; k_data_size = ROUND_UP(k_data_size, BYTES_PER_WORD); /* Keep word aligned */ if (k_data_seg == NULL) k_data_seg = (mem_word *) xmalloc (k_data_size); else k_data_seg = (mem_word *) realloc (k_data_seg, k_data_size); memclr (k_data_seg, k_data_size); k_data_seg_b = (BYTE_TYPE *) k_data_seg; k_data_seg_h = (short *) k_data_seg; k_data_top = K_DATA_BOT + k_data_size; k_data_size_limit = k_data_limit; text_modified = 1; data_modified = 1;}/* Free the storage used by the old instructions in memory. */static voidfree_instructions (instruction **inst, int n){ for ( ; n > 0; n --, inst ++) if (*inst) free_inst (*inst);}/* Expand the data segment by adding N bytes. */voidexpand_data (int addl_bytes){ int delta = ROUND_UP(addl_bytes, BYTES_PER_WORD); /* Keep word aligned */ int old_size = data_top - DATA_BOT; int new_size = old_size + delta; BYTE_TYPE *p; if ((addl_bytes < 0) || (new_size > data_size_limit)) { error ("Can't expand data segment by %d bytes to %d bytes\n", addl_bytes, new_size); run_error ("Use -ldata # with # > %d\n", new_size); } data_seg = (mem_word *) realloc (data_seg, new_size); if (data_seg == NULL) fatal_error ("realloc failed in expand_data\n"); data_seg_b = (BYTE_TYPE *) data_seg; data_seg_h = (short *) data_seg; data_top += delta; /* Zero new memory */ for (p = data_seg_b + old_size; p < data_seg_b + new_size; ) *p ++ = 0;}/* Expand the stack segment by adding N bytes. Can't use REALLOC since it copies from bottom of memory blocks and stack grows down from top of its block. */voidexpand_stack (int addl_bytes){ int delta = ROUND_UP(addl_bytes, BYTES_PER_WORD); /* Keep word aligned */ int old_size = STACK_TOP - stack_bot; int new_size = old_size + MAX (delta, old_size); mem_word *new_seg; mem_word *po, *pn; if ((addl_bytes < 0) || (new_size > stack_size_limit)) { error ("Can't expand stack segment by %d bytes to %d bytes\n", addl_bytes, new_size); run_error ("Use -lstack # with # > %d\n", new_size); } new_seg = (mem_word *) xmalloc (new_size); po = stack_seg + (old_size / BYTES_PER_WORD - 1); pn = new_seg + (new_size / BYTES_PER_WORD - 1); for ( ; po >= stack_seg ; ) *pn -- = *po --; for ( ; pn >= new_seg ; ) *pn -- = 0; free (stack_seg); stack_seg = new_seg; stack_seg_b = (BYTE_TYPE *) stack_seg; stack_seg_h = (short *) stack_seg; stack_bot -= (new_size - old_size);}/* Expand the kernel data segment by adding N bytes. */voidexpand_k_data (int addl_bytes){ int delta = ROUND_UP(addl_bytes, BYTES_PER_WORD); /* Keep word aligned */ int old_size = k_data_top - K_DATA_BOT; int new_size = old_size + delta; BYTE_TYPE *p; if ((addl_bytes < 0) || (new_size > k_data_size_limit)) { error ("Can't expand kernel data segment by %d bytes to %d bytes\n", addl_bytes, new_size); run_error ("Use -lkdata # with # > %d\n", new_size); } k_data_seg = (mem_word *) realloc (k_data_seg, new_size); if (k_data_seg == NULL) fatal_error ("realloc failed in expand_k_data\n"); k_data_seg_b = (BYTE_TYPE *) k_data_seg; k_data_seg_h = (short *) k_data_seg; k_data_top += delta; /* Zero new memory */ for (p = k_data_seg_b + old_size / BYTES_PER_WORD; p < k_data_seg_b + new_size / BYTES_PER_WORD; ) *p ++ = 0;}/* Access memory */void*mem_reference(mem_addr addr){ if ((addr >= TEXT_BOT) && (addr < text_top)) return addr - TEXT_BOT + (char*) text_seg; else if ((addr >= DATA_BOT) && (addr < data_top)) return addr - DATA_BOT + (char*) data_seg; else if ((addr >= stack_bot) && (addr < STACK_TOP)) return addr - stack_bot + (char*) stack_seg; else if ((addr >= K_TEXT_BOT) && (addr < k_text_top)) return addr - K_TEXT_BOT + (char*) k_text_seg; else if ((addr >= K_DATA_BOT) && (addr < k_data_top)) return addr - K_DATA_BOT + (char*) k_data_seg; else { run_error ("Memory address out of bounds\n"); return NULL; }}instruction*read_mem_inst(mem_addr addr){ if ((addr >= TEXT_BOT) && (addr < text_top) && !(addr & 0x3)) return text_seg [(addr - TEXT_BOT) >> 2]; else if ((addr >= K_TEXT_BOT) && (addr < k_text_top) && !(addr & 0x3)) return k_text_seg [(addr - K_TEXT_BOT) >> 2]; else return bad_text_read (addr);}reg_wordread_mem_byte(mem_addr addr){ if ((addr >= DATA_BOT) && (addr < data_top)) return data_seg_b [addr - DATA_BOT]; else if ((addr >= stack_bot) && (addr < STACK_TOP)) return stack_seg_b [addr - stack_bot]; else if ((addr >= K_DATA_BOT) && (addr < k_data_top)) return k_data_seg_b [addr - K_DATA_BOT]; else return bad_mem_read (addr, 0);}reg_wordread_mem_half(mem_addr addr){ if ((addr >= DATA_BOT) && (addr < data_top) && !(addr & 0x1)) return data_seg_h [(addr - DATA_BOT) >> 1]; else if ((addr >= stack_bot) && (addr < STACK_TOP) && !(addr & 0x1)) return stack_seg_h [(addr - stack_bot) >> 1]; else if ((addr >= K_DATA_BOT) && (addr < k_data_top) && !(addr & 0x1)) return k_data_seg_h [(addr - K_DATA_BOT) >> 1]; else return bad_mem_read (addr, 0x1);}reg_wordread_mem_word(mem_addr addr){ if ((addr >= DATA_BOT) && (addr < data_top) && !(addr & 0x3)) return data_seg [(addr - DATA_BOT) >> 2]; else if ((addr >= stack_bot) && (addr < STACK_TOP) && !(addr & 0x3)) return stack_seg [(addr - stack_bot) >> 2]; else if ((addr >= K_DATA_BOT) && (addr < k_data_top) && !(addr & 0x3)) return k_data_seg [(addr - K_DATA_BOT) >> 2]; else return bad_mem_read (addr, 0x3);}voidset_mem_inst(mem_addr addr, instruction* inst){ text_modified = 1; if ((addr >= TEXT_BOT) && (addr < text_top) && !(addr & 0x3)) text_seg [(addr - TEXT_BOT) >> 2] = inst; else if ((addr >= K_TEXT_BOT) && (addr < k_text_top) && !(addr & 0x3)) k_text_seg [(addr - K_TEXT_BOT) >> 2] = inst; else bad_text_write (addr, inst);}voidset_mem_byte(mem_addr addr, reg_word value){ data_modified = 1; if ((addr >= DATA_BOT) && (addr < data_top)) data_seg_b [addr - DATA_BOT] = (BYTE_TYPE) value;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -