5f40e9ebfed9d89d4e5c388412380be5aa62f64e
[fw/sdcc] / sim / ucsim / sim.src / test_mem_speed.cc
1 #include <signal.h>
2 #include <unistd.h>
3 #include <stdio.h>
4
5 #include "memcl.h"
6 #include "hwcl.h"
7
8 #include "newcmdcl.h"
9
10 static int go;
11
12 static void
13 alarmed(int sig)
14 {
15   go= 0;
16   signal(sig, alarmed);
17 }
18
19 class cl_hw_test: public cl_hw
20 {
21 public:
22   cl_hw_test(void): cl_hw(0, HW_PORT, 0, "0") {}
23   virtual t_mem r(class cl_cell *cell, t_addr addr);
24   virtual void write(class cl_mem *mem, t_addr addr, t_mem *val);
25 };
26
27 t_mem
28 cl_hw_test::r(class cl_cell *cell, t_addr addr)
29 {
30   return(cell->get());
31 }
32
33 void
34 cl_hw_test::write(class cl_mem *mem, t_addr addr, t_mem *val)
35 {
36 }
37
38 double
39 do_rw_test(class cl_mem *mem, int time)
40 {
41   double counter;
42   t_addr a;
43   t_mem d;
44
45   go= 1;
46   counter= 0;
47   alarm(time);
48   while (go)
49     for (a= 0; go && a < mem->size; a++)
50       {
51         t_mem d2;
52         for (d2= 0; go && d2 <= 255; d2++)
53           {
54             d2= mem->write(a, d2);
55             d= mem->read(a);
56             if (d != d2)
57               printf("%"_M_"d written to mem and %"_M_"d read back!\n", d2, d);
58             counter+= 1;
59           }
60       }
61   return(counter);
62 }
63
64 int
65 main(void)
66 {
67   int i;
68   class cl_mem *mem;
69   class cl_m *m2;
70   class cl_console *con;
71
72   signal(SIGALRM, alarmed);
73   con= new cl_console(stdin, stdout, 0);
74
75   mem= new cl_mem(MEM_SFR, "egy", 0x10000, 8);
76   mem->init();
77   printf("%g operations on classic memory within 5 sec\n",
78          do_rw_test(mem, 5));
79   //mem->dump(con);
80
81   m2= new cl_m(MEM_TYPES, "test", 0x10000, 8);
82   m2->init();
83   printf("%g operations on new memory within 5 sec\n",
84          do_rw_test(m2, 5));
85
86   class cl_hw_test *hw= new cl_hw_test();
87   for (i= 0; i < 0x10000; i++)
88     {
89       class cl_cell *c= m2->get_cell(i);
90       if (c)
91         c->add_hw(hw);
92     }
93   printf("%g operations on new memory within 5 sec with hw read\n",
94          do_rw_test(m2, 5));
95   //m2->dump(con);
96
97   return(0);
98 }