OSSIM – Objective 2 OSSIM – Objective 2 Overview Overview Develop functions which initialize the memory and Develop functions which initialize the memory and memory management data structures of the simulator memory management data structures of the simulator simulate the basic functions of the CPU, as well simulate the basic functions of the CPU, as well as provide more simulation output. as provide more simulation output. Boot Boot: The simulator will initally call your Boot() function The simulator will initally call your Boot() function that will load programs from boot.dat that are stored that will load programs from boot.dat that are stored in the format described in intro.doc. in the format described in intro.doc. Boot() will also initialize the data structures Boot() will also initialize the data structures responsible for managing the simulated memory and responsible for managing the simulated memory and will call Get_Instr() repeatedly to read instructions will call Get_Instr() repeatedly to read instructions from boot.dat and will store them in the simulated from boot.dat and will store them in the simulated memory. memory. Finally, Boot() will call Display_pgm() for each Finally, Boot() will call Display_pgm() for each program in boot.dat to output it to simout. program in boot.dat to output it to simout.
OSSIM – Objective 2. Overview Develop functions which initialize the memory and memory management data structures of the simulator simulate the basic functions of the CPU, as well as provide more simulation output. Boot : - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Develop functions which initialize the memory and memory Develop functions which initialize the memory and memory management data structures of the simulatormanagement data structures of the simulator
simulate the basic functions of the CPU, as well as provide more simulate the basic functions of the CPU, as well as provide more simulation output. simulation output.
BootBoot:: The simulator will initally call your Boot() function that will load The simulator will initally call your Boot() function that will load
programs from boot.dat that are stored in the format described in programs from boot.dat that are stored in the format described in intro.doc. intro.doc.
Boot() will also initialize the data structures responsible for managing the Boot() will also initialize the data structures responsible for managing the simulated memory and will call Get_Instr() repeatedly to read instructions simulated memory and will call Get_Instr() repeatedly to read instructions from boot.dat and will store them in the simulated memory. from boot.dat and will store them in the simulated memory.
Finally, Boot() will call Display_pgm() for each program in boot.dat to Finally, Boot() will call Display_pgm() for each program in boot.dat to output it to simout. output it to simout.
After After Boot()Boot() has completed has completed XPGM()XPGM() will be called which will be called which simulates a context switch and then calls simulates a context switch and then calls Cpu()Cpu(). .
Cpu()Cpu() sets the memory address register (MAR) to the value sets the memory address register (MAR) to the value passed to it by passed to it by XPGM(),XPGM(), this will be 0 initially. this will be 0 initially.
Cpu()Cpu() then calls then calls Fetch()Fetch() to get the next instruction to to get the next instruction to execute from memory. execute from memory.
Fetch()Fetch() calls calls Mu()Mu() to determine the physical location in to determine the physical location in memory of the requested instruction and uses the result to memory of the requested instruction and uses the result to return the instruction to return the instruction to Cpu()Cpu(). .
Cpu()Cpu() then handles the instruction accordingly depending then handles the instruction accordingly depending on the operation. This entire cycle then repeats until there on the operation. This entire cycle then repeats until there are no more simulation events.are no more simulation events.
OSSIM - Objective 2OSSIM - Objective 2
Call Tree of function Call Tree of function youyou write: write: Program:Program:
Each segment_type Each segment_type segment length in instructions (seglen) segment length in instructions (seglen) the base address (membase) in memory where the the base address (membase) in memory where the
segment begins.segment begins.
OSSIM – Objective 2OSSIM – Objective 2
Important VariablesImportant Variables MEMMEM
A pointer to MEMSIZE array of data types of A pointer to MEMSIZE array of data types of type struct instr_type (defined in osdefs.h).type struct instr_type (defined in osdefs.h).
FreeMemFreeMem
freememsegsize = 5segptr*next
segsize = 5segptr*next
OSSIM - Objective 2OSSIM - Objective 2
Global Variables: Declared in Global Variables: Declared in extern.h:extern.h:
This function is called from simulator.c and reads This function is called from simulator.c and reads from boot.dat and initializes the memory and from boot.dat and initializes the memory and memory management data structures. memory management data structures.
The programs from boot.dat represent the OS and The programs from boot.dat represent the OS and are loaded into the upper half of MEMMAP.are loaded into the upper half of MEMMAP.
Read the file boot.dat whose file pointer is PROGM_FILE[BOOT] and whose Read the file boot.dat whose file pointer is PROGM_FILE[BOOT] and whose format is given in intro.doc. You will have to check for PROGRAM on the first format is given in intro.doc. You will have to check for PROGRAM on the first line and read in the number of programs in the file. line and read in the number of programs in the file.
For each program:For each program: Read in each segment and store the access bits and number of instructions.Read in each segment and store the access bits and number of instructions. With the program and segment data initialize MEMMAP starting at segment With the program and segment data initialize MEMMAP starting at segment
MAXSEGMENTS. The size of MEMMAP is 2 * MAXSEGMENTS. The MAXSEGMENTS. The size of MEMMAP is 2 * MAXSEGMENTS. The first half is reserved for user memory, while the upper half is reserved for the first half is reserved for user memory, while the upper half is reserved for the OS.OS.
Call Call Get_Instr()Get_Instr() repeatedly to read instructions from boot.dat and update repeatedly to read instructions from boot.dat and update TotalFree and FreeMem based on the number of instructions read from TotalFree and FreeMem based on the number of instructions read from boot.dat. boot.dat.
Call Call Display_pgm()Display_pgm() to display each program. to display each program.
unsigned char accbitsunsigned int seglenunsigned long membase
unsigned char accbitsunsigned int seglenunsigned long membase
This function reads the next instruction from file (fp) into This function reads the next instruction from file (fp) into instr. instr.
The external file (fp) is PROGM_FILE[pgmid]. The external file (fp) is PROGM_FILE[pgmid]. The format of the file is a series of statements of the form: The format of the file is a series of statements of the form:
OPCODE x y z where the form and type of the operands OPCODE x y z where the form and type of the operands (x,y,z) depend on OPCODE. (x,y,z) depend on OPCODE.
Each instruction starts on a new line. There is more Each instruction starts on a new line. There is more information in intro.doc on the format of boot.dat.information in intro.doc on the format of boot.dat.
Read the instructions from boot.dat (PROGM_FILE[BOOT]).Read the instructions from boot.dat (PROGM_FILE[BOOT]). Convert the instruction to its opcode by using the lookup table Convert the instruction to its opcode by using the lookup table
opidtab which is defined in simulator.c if the instruction is not a opidtab which is defined in simulator.c if the instruction is not a device. device. If it is a device look up its opcode in the devid field of the devtable.If it is a device look up its opcode in the devid field of the devtable.
After determining the opcode set the operand as described in After determining the opcode set the operand as described in intro.doc.intro.doc.
Each instruction has a field for the opcode and operand. Each instruction has a field for the opcode and operand. The operand field is a C union and depending on the opcode, only The operand field is a C union and depending on the opcode, only
certain fields will be used in the operand. certain fields will be used in the operand. The address field is used for REQ and JUMP instructions The address field is used for REQ and JUMP instructions The count field is used for SKIP instructionsThe count field is used for SKIP instructions The burst field is used for SIO, WIO, and END instructions, and The burst field is used for SIO, WIO, and END instructions, and The bytes field is used for device instructions. The bytes field is used for device instructions.
OSSIM – Objective 2OSSIM – Objective 2 Data structuresData structures
This function simulates the basic functions of a CPU. This function simulates the basic functions of a CPU. It fetches instructions from memory and handles them It fetches instructions from memory and handles them
accordingly.accordingly.
DirectionsDirections In a loop:In a loop: SetMARSetMAR(&CPU.pc)(&CPU.pc) FetchFetch(&IREG); (&IREG);
If Fetch() returns negative, a FAULT has occurred. In this If Fetch() returns negative, a FAULT has occurred. In this case, CPU() returns. Otherwise, continue.case, CPU() returns. Otherwise, continue.
3.3.Decode IREG and execute the instruction:Decode IREG and execute the instruction:i.e. Look at IREG.opcodei.e. Look at IREG.opcode
SIO, WIO, and END instructions: compute the deltaT defined by the operand, add to CLOCK to get SIO, WIO, and END instructions: compute the deltaT defined by the operand, add to CLOCK to get future event time, and add this new event to the event list, i.e get the appropriate event number, and future event time, and add this new event to the event list, i.e get the appropriate event number, and call Add_event( &deltaT, event, agent_code) (See note 3). Increment CPU.pc.offset by 2 and return call Add_event( &deltaT, event, agent_code) (See note 3). Increment CPU.pc.offset by 2 and return
FOR SKIP, evaluate the operand (i.e. look at IREG.operand.count). If the operand of IREG changes FOR SKIP, evaluate the operand (i.e. look at IREG.operand.count). If the operand of IREG changes (i.e. count > 0), you must (i.e. count > 0), you must decrement IREG.operand.count and decrement IREG.operand.count and update MEM by a call to update MEM by a call to Write()Write() with the modified IREG. Increment CPU.pc.offset by 2 if the next instruction is to be skipped with the modified IREG. Increment CPU.pc.offset by 2 if the next instruction is to be skipped (count > 0 ) and repeat from step (1).(count > 0 ) and repeat from step (1).
Otherwise, Otherwise, Fetch()Fetch() the JUMP instruction at CPU.pc.offset+1, i.e. increment CPU.pc.offset, call the JUMP instruction at CPU.pc.offset+1, i.e. increment CPU.pc.offset, call SetMAR(&CPU.pc), and then Fetch(). Execute the JUMP by placing its operand in CPU.pc and SetMAR(&CPU.pc), and then Fetch(). Execute the JUMP by placing its operand in CPU.pc and repeat from step (1).repeat from step (1).
NOTE 1: you will find the function Burst_time(deltaT, IREG.operand.burst) in SIMLATOR.C of use NOTE 1: you will find the function Burst_time(deltaT, IREG.operand.burst) in SIMLATOR.C of use when converting from CPU cycles to simulation time. when converting from CPU cycles to simulation time.
2: Add_time( &CLOCK, &deltaT) 2: Add_time( &CLOCK, &deltaT) 3: NOTE: For OBJECTIVE 23: NOTE: For OBJECTIVE 2 you should use a special agent code (0) to identify the BOOT you should use a special agent code (0) to identify the BOOT
program. For all other OBJECTIVES the agent code should be: CPU.actvpcb->termnl+1. Ue this in program. For all other OBJECTIVES the agent code should be: CPU.actvpcb->termnl+1. Ue this in Add_event() Add_event()
OSSIM – Objective 2OSSIM – Objective 2
void SetMar(struct addr_type *addr)void SetMar(struct addr_type *addr) This function sets a global variable MAR This function sets a global variable MAR
representing the memory address registerrepresenting the memory address register.. Directions:Directions:
Set the MAR with the value of (addr) and return. Set the MAR with the value of (addr) and return.
This function must be called to define the This function must be called to define the logical MEM address of the next logical MEM address of the next Fetch(),Fetch(), Read(),Read(), or or Write()Write() operation on memory. operation on memory. int segmentunsigned int offset
This function will try and fetch an instruction from This function will try and fetch an instruction from memory and store it in instr.memory and store it in instr.
Directions:Directions: This function calls This function calls Mu(void)Mu(void) to validate and map the to validate and map the
logical address in MAR to a physical address. logical address in MAR to a physical address. Mu()Mu() will will return a negative value if some kind of FAULT was return a negative value if some kind of FAULT was generated. In this case, Fetch() returns -1. generated. In this case, Fetch() returns -1.
If Mu() returns a non-negative value, say x, then Fetch If Mu() returns a non-negative value, say x, then Fetch sets *instr = MEM[x] and returns +1. sets *instr = MEM[x] and returns +1.
OSSIM – Objective 2OSSIM – Objective 2
int Mu(void)int Mu(void) This function simulates the address translation This function simulates the address translation
hardware of the memory unit.hardware of the memory unit. It uses the contents of MAR = [s(=segment), It uses the contents of MAR = [s(=segment),
d(=offset)] as the logical address to be translated d(=offset)] as the logical address to be translated to a physical address, x.to a physical address, x.
First compute the effective entry in MEMMAP.First compute the effective entry in MEMMAP. Set SEG = s + CPU.mode*MAXSEGMENTS. Set SEG = s + CPU.mode*MAXSEGMENTS. This forces the upper half of the MEMMAP to be used if CPU.mode == 1 This forces the upper half of the MEMMAP to be used if CPU.mode == 1
(privileged mode) and the lower half if CPU.mode != 1 (user mode). (privileged mode) and the lower half if CPU.mode != 1 (user mode).
If MEMMAP[SEG].accbits == 0x00, then generate an SEGFAULT event at If MEMMAP[SEG].accbits == 0x00, then generate an SEGFAULT event at the current CLOCK time and add it to the event_list using Add_event(). the current CLOCK time and add it to the event_list using Add_event(). return -1. return -1.
use Agent = CPU.actvpcb->termnl+1. use Agent = CPU.actvpcb->termnl+1. (agent = 0 for objective 2)(agent = 0 for objective 2)
If MEMMAP[SEG].seglen <= d, then generate an ADRFAULT event at the If MEMMAP[SEG].seglen <= d, then generate an ADRFAULT event at the
current CLOCK time and add it to the event_list. return -1.current CLOCK time and add it to the event_list. return -1. use Agent = CPU.actvpcb->termnl+1. use Agent = CPU.actvpcb->termnl+1. (agent = 0 for objective 2)(agent = 0 for objective 2)
return x = MEMMAP[SEG].membase + d. return x = MEMMAP[SEG].membase + d.
OSSIM – Objective 2OSSIM – Objective 2
void XPGM(struct state_type *state)void XPGM(struct state_type *state) This function simulates a privileged instruction (in this This function simulates a privileged instruction (in this
case, loading and starting your program) causing a context case, loading and starting your program) causing a context switch.switch.
Directions:Directions: switch placing a user program in execution. It does this switch placing a user program in execution. It does this
by copying (state->mode) into CPU.mode and (state-by copying (state->mode) into CPU.mode and (state->pc) into CPU.pc. >pc) into CPU.pc.
After the state of the CPU has been redefined, the CPU After the state of the CPU has been redefined, the CPU resumes execution at CPU.pc -- this is implemented by resumes execution at CPU.pc -- this is implemented by simply calling the function, simply calling the function, Cpu().Cpu().
This function is identical to Fetch()This function is identical to Fetch() DirectionsDirections
This function calls Mu() to validate and map the logical address in MAR to This function calls Mu() to validate and map the logical address in MAR to a physical address. Mu() will return a negative value if some kind of a physical address. Mu() will return a negative value if some kind of FAULT was generated. In this case, Read() returns -1. FAULT was generated. In this case, Read() returns -1.
If Mu() returns a non-negative value, say x, then Read sets *instr = MEM[x] If Mu() returns a non-negative value, say x, then Read sets *instr = MEM[x] and returns +1. and returns +1.
int Write(struct instr_type *instr)int Write(struct instr_type *instr) This function is similar to FetchThis function is similar to Fetch
DirectionsDirections This function calls Mu() to validate and map the logical address in MAR to This function calls Mu() to validate and map the logical address in MAR to
a physical address. Mu() will return a negative value if some kind of a physical address. Mu() will return a negative value if some kind of FAULT was generated. In this case, Write() returns -1FAULT was generated. In this case, Write() returns -1
If Mu() returns a non-negative value, say x, then Write If Mu() returns a non-negative value, say x, then Write setssets MEM[x] = MEM[x] = *instr *instr and returns +1. and returns +1.
OSSIM – Objective 2OSSIM – Objective 2 void Display_pgm(struct segment_type *segtab, int void Display_pgm(struct segment_type *segtab, int
numseg, struct pcb_type *pcb)numseg, struct pcb_type *pcb) This function outputs a program to simout.This function outputs a program to simout. Use this function after every program load. Use this function after every program load. Use segtab, segment table * to locate each segment. Use the Use segtab, segment table * to locate each segment. Use the
provided sample output in intro.doc as an example of the format of provided sample output in intro.doc as an example of the format of the dump and what information should be output. the dump and what information should be output.
Note: Note: Be sure to print the process and program names as Be sure to print the process and program names as "BOOT" in Objective 2 since pcb will always be null."BOOT" in Objective 2 since pcb will always be null.
That is, test whether pcb is null. If it is, output “BOOT” for That is, test whether pcb is null. If it is, output “BOOT” for the process and program names.the process and program names.
unsigned char accbitsunsigned int seglenunsigned long membase
unsigned char accbitsunsigned int seglenunsigned long membase