aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpacien2018-03-28 17:02:43 +0200
committerpacien2018-03-28 17:02:43 +0200
commit0441d28435e8db1d60c1ae30c81e3a54f12149c8 (patch)
tree5b9333551725b440acde6d5b16f278e5ffa5bd93
parent6a979da2d6c2cf3347c6cad0439518a5f084648e (diff)
downloadmempager-simulator-0441d28435e8db1d60c1ae30c81e3a54f12149c8.tar.gz
Adapt for fixed specs
-rw-r--r--fifo.py10
-rw-r--r--mem.py17
-rw-r--r--myMemory.18
-rwxr-xr-xmyMemory.py2
4 files changed, 24 insertions, 13 deletions
diff --git a/fifo.py b/fifo.py
index 7fa55a0..61e70f5 100644
--- a/fifo.py
+++ b/fifo.py
@@ -10,7 +10,6 @@ class FifoPaginator:
10 def __init__(self, mem): 10 def __init__(self, mem):
11 self.mem = mem 11 self.mem = mem
12 self._main_cursor = 0 12 self._main_cursor = 0
13 self._virt_cursor = 0
14 13
15 def _put_main(self, page): 14 def _put_main(self, page):
16 overwritten = self.mem.main[self._main_cursor] 15 overwritten = self.mem.main[self._main_cursor]
@@ -18,13 +17,10 @@ class FifoPaginator:
18 self._main_cursor = (self._main_cursor + 1) % len(self.mem.main) 17 self._main_cursor = (self._main_cursor + 1) % len(self.mem.main)
19 return overwritten 18 return overwritten
20 19
21 def _put_virt(self, page):
22 self.mem.virt[self._virt_cursor] = page
23 self._virt_cursor += 1
24
25 def load(self, page): 20 def load(self, page):
26 if not self.mem.has_page(page): raise IndexError 21 if not self.mem.page_in_range(page): raise IndexError
27 if page in self.mem.main: return 22 if page in self.mem.main: return
23 if page in self.mem.virt: self.mem.remove_virt(page)
28 overwritten = self._put_main(page) 24 overwritten = self._put_main(page)
29 if overwritten not in self.mem.virt: self._put_virt(overwritten) 25 if overwritten is not None: self.mem.put_virt(overwritten)
30 26
diff --git a/mem.py b/mem.py
index 7137ede..c83d69d 100644
--- a/mem.py
+++ b/mem.py
@@ -8,6 +8,7 @@ class Memory:
8 def __init__(self, main_size, virt_size): 8 def __init__(self, main_size, virt_size):
9 self.main = [None] * main_size 9 self.main = [None] * main_size
10 self.virt = [None] * virt_size 10 self.virt = [None] * virt_size
11 self._virt_cursor = 0
11 12
12 def _mem_to_string(self, mem): 13 def _mem_to_string(self, mem):
13 return ''.join(['%d' % n if n is not None else 'x' for n in mem]) 14 return ''.join(['%d' % n if n is not None else 'x' for n in mem])
@@ -18,6 +19,18 @@ class Memory:
18 def dump_virt(self): 19 def dump_virt(self):
19 return self._mem_to_string(self.virt) 20 return self._mem_to_string(self.virt)
20 21
21 def has_page(self, page): 22 def page_in_range(self, page):
22 return 0 <= page < len(self.virt) 23 return 0 <= page < len(self.main + self.virt)
24
25 def put_virt(self, page):
26 self.virt[self._virt_cursor] = page
27 self._virt_cursor += 1
28
29 def remove_virt(self, page):
30 for i in range(len(self.virt)):
31 if self.virt[i] == page:
32 self._virt_cursor -= 1
33 self.virt[i] = self.virt[self._virt_cursor]
34 self.virt[self._virt_cursor] = None
35 break
23 36
diff --git a/myMemory.1 b/myMemory.1
index d7a5827..1093e66 100644
--- a/myMemory.1
+++ b/myMemory.1
@@ -62,18 +62,20 @@ can only handle new style baz segments. COBOL
62object libraries are not supported in this version. 62object libraries are not supported in this version.
63.SH EXAMPLE 63.SH EXAMPLE
64.nf 64.nf
65% ./myMemory.py -s 3 -v 8 -a fifo 65% ./myMemory.py -s 3 -v 5 -a fifo
66> 7 7xx 66> 7 7xx
67> 0 70x 67> 0 70x
68> 1 701 68> 1 701
69> 2 201 69> 2 201
70> p 201 7xxxx
70> 0 201 71> 0 201
71> 3 231 72> 3 231
72> p 231 70xxxxxx 73> p 231 70xxx
73> 0 230 74> 0 230
75> p 230 71xxx
74> 4 430 76> 4 430
75> 2 420 77> 2 420
76> p 420 70123xxx 78> p 420 713xx
77> x 79> x
78.fi 80.fi
79.SH AUTHORS 81.SH AUTHORS
diff --git a/myMemory.py b/myMemory.py
index dd2aaa6..2439aae 100755
--- a/myMemory.py
+++ b/myMemory.py
@@ -25,7 +25,7 @@ def main(main_size, virt_size, paginator):
25 try: 25 try:
26 pag.load(int(cmd)) 26 pag.load(int(cmd))
27 output(cmd, mem.dump_main()) 27 output(cmd, mem.dump_main())
28 except: 28 except IndexError:
29 output(cmd, "Error: invalid page.") 29 output(cmd, "Error: invalid page.")
30 elif cmd == 'p': 30 elif cmd == 'p':
31 output(cmd, mem.dump_main() + '\t' + mem.dump_virt()) 31 output(cmd, mem.dump_main() + '\t' + mem.dump_virt())