aboutsummaryrefslogtreecommitdiff
path: root/fifo.py
blob: 18818706e3597462b86504fea4c8b49614ca460c (plain)
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
#!/usr/bin/env python3

# UPEM / System programming / Project: Memory paging simulator
# Pacien TRAN-GIRARD, Adam NAILI

from mem import Memory
from pageexception import *


class FifoPaginator:
  def __init__(self, mem):
    self.mem = mem
    self._main_cursor = 0

  def _put_main(self, page):
    overwritten = self.mem.main[self._main_cursor]
    self.mem.main[self._main_cursor] = page
    self._main_cursor = (self._main_cursor + 1) % len(self.mem.main)
    return overwritten

  def load(self, page):
    if not self.mem.page_in_range(page): raise PageException
    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 is not None: self.mem.put_virt(overwritten)