This repository has been archived by the owner on Oct 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.c
executable file
·101 lines (95 loc) · 2.11 KB
/
loader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "loader.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static uint32_t avail_pid = 1;
#define OPT_CALC "calc"
#define OPT_ALLOC "alloc"
#define OPT_FREE "free"
#define OPT_READ "read"
#define OPT_WRITE "write"
static enum ins_opcode_t get_opcode(char *opt)
{
if (!strcmp(opt, OPT_CALC))
{
return CALC;
}
else if (!strcmp(opt, OPT_ALLOC))
{
return ALLOC;
}
else if (!strcmp(opt, OPT_FREE))
{
return FREE;
}
else if (!strcmp(opt, OPT_READ))
{
return READ;
}
else if (!strcmp(opt, OPT_WRITE))
{
return WRITE;
}
else
{
printf("Opcode: %s\n", opt);
exit(1);
}
}
struct pcb_t *load(const char *path)
{
/* Create new PCB for the new process */
struct pcb_t *proc = (struct pcb_t *)malloc(sizeof(struct pcb_t));
proc->pid = avail_pid;
avail_pid++;
proc->seg_table =
(struct seg_table_t *)malloc(sizeof(struct seg_table_t));
proc->bp = PAGE_SIZE;
proc->pc = 0;
/* Read process code from file */
FILE *file;
if ((file = fopen(path, "r")) == NULL)
{
printf("Cannot find process description at '%s'\n", path);
exit(1);
}
char opcode[10];
proc->code = (struct code_seg_t *)malloc(sizeof(struct code_seg_t));
fscanf(file, "%u %u", &proc->priority, &proc->code->size);
proc->code->text = (struct inst_t *)malloc(
sizeof(struct inst_t) * proc->code->size);
uint32_t i = 0;
for (i = 0; i < proc->code->size; i++)
{
fscanf(file, "%s", opcode);
proc->code->text[i].opcode = get_opcode(opcode);
switch (proc->code->text[i].opcode)
{
case CALC:
break;
case ALLOC:
fscanf(
file,
"%u %u\n",
&proc->code->text[i].arg_0,
&proc->code->text[i].arg_1);
break;
case FREE:
fscanf(file, "%u\n", &proc->code->text[i].arg_0);
break;
case READ:
case WRITE:
fscanf(
file,
"%u %u %u\n",
&proc->code->text[i].arg_0,
&proc->code->text[i].arg_1,
&proc->code->text[i].arg_2);
break;
default:
printf("Opcode: %s\n", opcode);
exit(1);
}
}
return proc;
}