Exemplo n.º 1
0
        public static MemBlock ReadBlock(int addr)
        {
            MemBlock b = FindBlock(addr);

            if (b == null)
            {
                // Simulate reading from main memory
                b = new MemBlock();
                AddBlock(addr, b);
            }
            return(b);
        }
Exemplo n.º 2
0
        public static void WriteBlock(int addr, MemBlock b)
        {
            MemBlock b2 = FindBlock(addr);

            if (b2 == null)
            {
                AddBlock(addr, b);
            }
            else
            {
                b2.someValue = b.someValue;
            }
        }
Exemplo n.º 3
0
        static void AddBlock(int addr, MemBlock b)
        {
            if (GetCount() > MAX_COUNT)
            {
                KeyValuePair <DateTime, int> kp = lru.First();
                lru.Remove(kp.Key);
                dict.Remove(kp.Value);
                Console.WriteLine("REMOVE: time = {0}  addr = {1}", kp.Key.ToString("ffffff"), kp.Value);
            }

            b.lastUsedTime = DateTime.Now;
            dict[addr]     = b;
            lru.Add(b.lastUsedTime, addr);
            Console.WriteLine("ADD: time = {0}  addr = {1}", b.lastUsedTime.ToString("ffffff"), addr);
        }
Exemplo n.º 4
0
 static MemBlock FindBlock(int addr)
 {
     if (dict.ContainsKey(addr))
     {
         MemBlock b = dict[addr];
         lru.Remove(b.lastUsedTime);
         b.lastUsedTime = DateTime.Now;
         dict[addr]     = b; // updated
         lru.Add(b.lastUsedTime, addr);
         Console.WriteLine("FOUND: {0}", addr);
         return(b);
     }
     Console.WriteLine("NOT FOUND: {0}", addr);
     return(null);
 }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            Random rn = new Random();

            for (int i = 0; i < 25; i++)
            {
                Thread.Sleep(3);                   // make time stamps distint

                int addr = -1;
                if ((rn.Next() % 2) == 0)
                {
                    addr = GetExistingAddressForTest();
                }

                if (addr == -1)
                {
                    addr = rn.Next(1000, 9999);
                }

                int      op = (rn.Next() % 2);      // decide read or write
                MemBlock b;

                if (op == 0)
                {
                    b = ReadBlock(addr);
                }
                else
                {
                    b = new MemBlock();
                    WriteBlock(addr, b);
                }
            }

            // print final status
            Console.WriteLine("FINAL CACHE");
            foreach (KeyValuePair <DateTime, int> kp in lru)
            {
                Console.WriteLine("{0}:{1}", kp.Key.ToString("ffffff"), kp.Value);
            }
        }