OSDN Git Service

* public snapshot of sid simulator
[pf3gnuchains/pf3gnuchains3x.git] / sid / component / mapper / testsuite / busif.cxx
1 // busif.cxx - a component for testing word_bus<>.  -*- C++ -*-
2
3 // Copyright (C) 1999, 2000 Red Hat.
4 // This file is part of SID and is licensed under the GPL.
5 // See the file COPYING.SID for conditions for redistribution.
6
7 #include <string>
8 #include <iostream>
9
10 #include <cstdio>
11
12 #include <sidcomp.h>
13 #include <sidso.h>
14 #include <sidcomputil.h>
15 #include <sidtypes.h>
16
17 #include <sidbusutil.h>
18
19 using namespace sid;
20 using namespace sidutil;
21 using namespace std;
22
23 template <typename DataType>
24 class Busif : public virtual component,
25             public no_accessor_component,
26             public no_attribute_component,
27             public no_relation_component,
28             public fixed_bus_map_component,
29             public no_pin_component
30 {
31 private:
32   class BusInterface : public word_bus<DataType> {
33   private:
34     unsigned char mem[0x100];
35
36     typedef typename DataType::value_type internal_type;
37
38   public:
39     BusInterface() {
40       for( int i=0; i<0x100; i++ ) mem[i] = i + 1;
41     }
42     
43     sid::bus::status word_write( host_int_4 addr, 
44                                  DataType mask,
45                                  DataType data ) {
46       host_int_4 a = addr * sizeof( internal_type );
47       internal_type m = mask;
48       internal_type d = data;
49
50       if( sizeof(internal_type) == 8 )
51         printf( "write>>addr %d mask 0x%016llx data 0x%llx<<write\n", a, m, d );
52       else
53         printf( "write>>addr %d mask 0x%08x data 0x%x<<write\n", a, m, d );
54
55       for( int i=0; i<sizeof(internal_type); i++ ) {
56         if( mask.read_byte( i ) ) {
57           if( data.read_byte( i ) != mem[a + i] ) {
58             printf( "mismatch in byte %d: %02x != %02x\n", i, 
59                         data.read_byte(i), mem[a+i] );
60           }
61         }
62       }
63
64       return sid::bus::ok;
65     }
66
67     sid::bus::status word_read( host_int_4 addr, 
68                                 DataType mask,
69                                 DataType& data ) {
70       host_int_4 a = addr * sizeof( internal_type );
71
72       for( int i=0; i<sizeof(internal_type); i++ ) {
73         if( mask.read_byte( i ) )
74           data.write_byte( i, mem[a + i] );
75       }
76
77       internal_type m = mask;
78       internal_type d = data;
79
80       if( sizeof(internal_type) == 8 )
81         printf( "read>>addr %d mask 0x%016llx data 0x%llx<<read\n", a, m, d );
82       else
83         printf( "read>>addr %d mask 0x%08x data 0x%x<<read\n", a, m, d );
84
85       return sid::bus::ok;
86     }
87   };
88
89   BusInterface bus;
90
91 public:
92
93   Busif() {
94     add_bus( "bus", &bus );
95   }
96
97   ~Busif() {}
98 };
99
100 static vector<string>
101 BusifListTypes() {
102   vector<string> types;
103   types.push_back(string("busif-big-1"));
104   types.push_back(string("busif-big-2"));
105   types.push_back(string("busif-big-4"));
106   types.push_back(string("busif-big-8"));
107   types.push_back(string("busif-little-1"));
108   types.push_back(string("busif-little-2"));
109   types.push_back(string("busif-little-4"));
110   types.push_back(string("busif-little-8"));
111   return types;
112 }
113
114 static component*
115 BusifCreate( const string& instance ) {
116   if( instance == "busif-big-1" )
117     return new Busif<big_int_1>();
118   else if( instance == "busif-little-1" )
119     return new Busif<little_int_1>();
120   else if( instance == "busif-big-2" )
121     return new Busif<big_int_2>();
122   else if( instance == "busif-little-2" )
123     return new Busif<little_int_2>();
124   else if( instance == "busif-big-4" )
125     return new Busif<big_int_4>();
126   else if( instance == "busif-little-4" )
127     return new Busif<little_int_4>();
128   else if( instance == "busif-big-8" )
129     return new Busif<big_int_8>();
130   else if( instance == "busif-little-8" )
131     return new Busif<little_int_8>();
132   else
133     return 0;
134 }
135
136 static void
137 BusifDelete( component* c ) {
138 }
139
140 // static object
141 extern const component_library busif_component_library;
142
143 const component_library busif_component_library DLLEXPORT = 
144 {
145   COMPONENT_LIBRARY_MAGIC,
146   & BusifListTypes, 
147   & BusifCreate,
148   & BusifDelete
149 };
150