From 0441d28435e8db1d60c1ae30c81e3a54f12149c8 Mon Sep 17 00:00:00 2001 From: pacien Date: Wed, 28 Mar 2018 17:02:43 +0200 Subject: Adapt for fixed specs --- fifo.py | 10 +++------- mem.py | 17 +++++++++++++++-- myMemory.1 | 8 +++++--- myMemory.py | 2 +- 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: def __init__(self, mem): self.mem = mem self._main_cursor = 0 - self._virt_cursor = 0 def _put_main(self, page): overwritten = self.mem.main[self._main_cursor] @@ -18,13 +17,10 @@ class FifoPaginator: self._main_cursor = (self._main_cursor + 1) % len(self.mem.main) return overwritten - def _put_virt(self, page): - self.mem.virt[self._virt_cursor] = page - self._virt_cursor += 1 - def load(self, page): - if not self.mem.has_page(page): raise IndexError + if not self.mem.page_in_range(page): raise IndexError if page in self.mem.main: return + if page in self.mem.virt: self.mem.remove_virt(page) overwritten = self._put_main(page) - if overwritten not in self.mem.virt: self._put_virt(overwritten) + if overwritten is not None: self.mem.put_virt(overwritten) diff --git a/mem.py b/mem.py index 7137ede..c83d69d 100644 --- a/mem.py +++ b/mem.py @@ -8,6 +8,7 @@ class Memory: def __init__(self, main_size, virt_size): self.main = [None] * main_size self.virt = [None] * virt_size + self._virt_cursor = 0 def _mem_to_string(self, mem): return ''.join(['%d' % n if n is not None else 'x' for n in mem]) @@ -18,6 +19,18 @@ class Memory: def dump_virt(self): return self._mem_to_string(self.virt) - def has_page(self, page): - return 0 <= page < len(self.virt) + def page_in_range(self, page): + return 0 <= page < len(self.main + self.virt) + + def put_virt(self, page): + self.virt[self._virt_cursor] = page + self._virt_cursor += 1 + + def remove_virt(self, page): + for i in range(len(self.virt)): + if self.virt[i] == page: + self._virt_cursor -= 1 + self.virt[i] = self.virt[self._virt_cursor] + self.virt[self._virt_cursor] = None + break 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 object libraries are not supported in this version. .SH EXAMPLE .nf -% ./myMemory.py -s 3 -v 8 -a fifo +% ./myMemory.py -s 3 -v 5 -a fifo > 7 7xx > 0 70x > 1 701 > 2 201 +> p 201 7xxxx > 0 201 > 3 231 -> p 231 70xxxxxx +> p 231 70xxx > 0 230 +> p 230 71xxx > 4 430 > 2 420 -> p 420 70123xxx +> p 420 713xx > x .fi .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): try: pag.load(int(cmd)) output(cmd, mem.dump_main()) - except: + except IndexError: output(cmd, "Error: invalid page.") elif cmd == 'p': output(cmd, mem.dump_main() + '\t' + mem.dump_virt()) -- cgit v1.2.3