21
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

  • Upload
    tyrell

  • View
    54

  • Download
    1

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: OSSIM – Objective 2

OSSIM – Objective 2OSSIM – Objective 2 OverviewOverview

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.

Page 2: OSSIM – Objective 2

OSSIM – Objective 2OSSIM – Objective 2 OverviewOverview

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.

Page 3: OSSIM – Objective 2

OSSIM - Objective 2OSSIM - Objective 2

Call Tree of function Call Tree of function youyou write: write: Program:Program:

Boot()Boot() Get_Instr()Get_Instr() Display_pgm()Display_pgm()

XPGM()XPGM() Cpu()Cpu()

Fetch()Fetch() Mu()Mu()

Page 4: OSSIM – Objective 2

OSSIM – Objective 2OSSIM – Objective 2

Important VariablesImportant Variables MEMMAPMEMMAP

A pointer to 2* MAXSEGMENTS of type struct segment_type A pointer to 2* MAXSEGMENTS of type struct segment_type User memoryUser memory

MEMMAP[0] ... MEMMAP[MAXSEGMENTS - 1]MEMMAP[0] ... MEMMAP[MAXSEGMENTS - 1] Kernel MemoryKernel Memory

MEMMAP[MAXSEGMENTS] ... MEMMAP[2 * MEMMAP[MAXSEGMENTS] ... MEMMAP[2 * MAXSEGMENTS - 1]MAXSEGMENTS - 1]

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.

Page 5: OSSIM – Objective 2

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

Page 6: OSSIM – Objective 2

OSSIM - Objective 2OSSIM - Objective 2

Global Variables: Declared in Global Variables: Declared in extern.h:extern.h:

extern FILE …*PROGM_FILE[];extern FILE …*PROGM_FILE[]; extern struct addr_type MAR; /* extern struct addr_type MAR; /*

Memory Address Register */Memory Address Register */

extern struct simtime CLOCK; /* extern struct simtime CLOCK; /* System Hardware Clock */System Hardware Clock */

int segmentunsigned int offset

int segmentunsigned int offset

addr_type

unsigned long secondsunsigned long nanosec

unsigned long secondsunsigned long nanosec

simtime

Page 7: OSSIM – Objective 2

OSSIM - Objective 2OSSIM - Objective 2

Global Variables:Declared in externs.h:extern struct cpu_type CPU; /* Central Processor */

unsigned long rateunsigned long CPU_burstunsigned char modestruct address_type pcstruct pcb_type *actvpcbstruct simtime busystruct simtime qwaitstruct simtime responsestruct simtime idleint servedint maxqint qlendouble utilizestruct pcb_list *readystruct pcb_list *tail

unsigned long rateunsigned long CPU_burstunsigned char modestruct address_type pcstruct pcb_type *actvpcbstruct simtime busystruct simtime qwaitstruct simtime responsestruct simtime idleint servedint maxqint qlendouble utilizestruct pcb_list *readystruct pcb_list *tail

cpu_typeThis pcb isnot in the

ready queue.It is the active

pcb!

pcb_type pcb_typepcb_type

pcb_list pcb_listpcb_list

struct pcb_type *pcbstruct pcb_list *nextstruct pcb_list *prev

struct pcb_type *pcbstruct pcb_list *nextstruct pcb_list *prev

pcb_list

Page 8: OSSIM – Objective 2

OSSIM – Objective 2OSSIM – Objective 2

void Boot(void)void Boot(void)

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.

Page 9: OSSIM – Objective 2

OSSIM – Objective 2OSSIM – Objective 2 Directions:Directions:

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

MEMMAP: array of segment_type

freememsegsize = 5segptr*next

segsize = 5segptr*next

Page 10: OSSIM – Objective 2

OSSIM – Objective 2OSSIM – Objective 2

void Get_Instr(int pgmid, struct instr_type *instr)void Get_Instr(int pgmid, struct instr_type *instr)

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.

Page 11: OSSIM – Objective 2

OSSIM – Objective 2OSSIM – Objective 2 Directions:Directions:

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.

Page 12: OSSIM – Objective 2

OSSIM – Objective 2OSSIM – Objective 2 Data structuresData structures

IREG: struct instr_type {IREG: struct instr_type {

unsigned char opcode;unsigned char opcode;

union opernd_type operand;union opernd_type operand;

};};

union opernd_type {union opernd_type {

struct addr_type address;struct addr_type address;

unsigned int count;unsigned int count;

unsigned long burst;unsigned long burst;

unsigned long bytes;unsigned long bytes;

};};

Page 13: OSSIM – Objective 2

OSSIM – Objective 2OSSIM – Objective 2 void Cpu(void)void Cpu(void)

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.

Page 14: OSSIM – Objective 2

OSSIM – Objective 2OSSIM – Objective 2   Directions:Directions:

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()

Page 15: OSSIM – Objective 2

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

int segmentunsigned int offset

addr_type

Page 16: OSSIM – Objective 2

OSSIM – Objective 2OSSIM – Objective 2 int Fetch(struct instr_type *instr)int Fetch(struct instr_type *instr)

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.

Page 17: OSSIM – Objective 2

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.

int segmentunsigned int offset

int segmentunsigned int offset

addr_type

Page 18: OSSIM – Objective 2

OSSIM – Objective 2OSSIM – Objective 2 Directions:Directions:

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.

Page 19: OSSIM – Objective 2

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().

Page 20: OSSIM – Objective 2

OSSIM – Objective 2OSSIM – Objective 2 int Read(struct instr_type *instr)int Read(struct instr_type *instr)

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.

Page 21: OSSIM – Objective 2

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

segment_type