OSDN Git Service

Enable to track git://github.com/monaka/binutils.git
[pf3gnuchains/pf3gnuchains3x.git] / sid / component / lcd / lcd-char-display.cxx
1 // lcd-char-display.cxx - description.  -*- C++ -*-
2
3 // Copyright (C) 1999, 2000, 2001, 2002 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 "config.h"
8
9 #include <sidcomp.h>
10 #include <sidso.h>
11 #include <sidpinutil.h>
12 #include <sidattrutil.h>
13 #include <sidcomputil.h>
14 #include <sidattrutil.h>
15 #include <sidwatchutil.h>
16
17 #ifdef HAVE__USR_INCLUDE_CURSES_H
18 // The following macro definition prevents curses.h from providing a typedef
19 // for bool on Solaris.
20 #define _BOOL 1
21 #include "/usr/include/curses.h"
22 #else
23 #ifdef HAVE_CURSES_H
24 #include <curses.h>
25 #endif
26 #endif
27
28 using std::vector;
29 using std::string;
30 using std::ostream;
31 using std::istream;
32 using sid::component;
33 using sid::host_int_2;
34 using sid::host_int_4;
35 using sid::component_library;
36 using sid::COMPONENT_LIBRARY_MAGIC;
37
38 using namespace sidutil;
39
40 class lcd_char_display : public virtual component,
41                          public fixed_pin_map_component,
42                          public fixed_attribute_map_component,
43                          public no_bus_component,
44                          public no_accessor_component,
45                          public no_relation_component
46 {
47 private:
48
49   friend class callback_pin<lcd_char_display>;
50   callback_pin<lcd_char_display> row_col_pin;
51   callback_pin<lcd_char_display> frame_pin;
52   callback_pin<lcd_char_display> init_pin;
53
54   host_int_2 width, height;
55
56   void set_pixel( host_int_4 row_col );
57
58   void new_frame( host_int_4 val );
59   void init( host_int_4 );
60
61 #if HAVE_CURSES_H && HAVE_CURSES_LIBRARY
62   WINDOW *w;
63 #endif
64
65   // Triggerpoint manager
66   friend class self_watcher<lcd_char_display>;
67   self_watcher<lcd_char_display> trigger_mgr;
68
69   // Virtual pin interfaces between self_watcher and fixed_pin_map_component
70   sid::component::status
71   pin_factory( const string& n ) { return trigger_mgr.create_virtual_pin(n); }
72
73   void
74   pin_junkyard( const string& n ) { trigger_mgr.destroy_virtual_pin( n ); }
75
76 public:
77
78   lcd_char_display();
79   ~lcd_char_display() throw ();
80
81   // Save & restore state
82   string save_state ();
83   component::status restore_state (const string& state);
84 };
85
86 lcd_char_display :: lcd_char_display() : 
87   row_col_pin( this, &lcd_char_display::set_pixel ),
88   frame_pin( this, &lcd_char_display::new_frame ),
89   init_pin( this, &lcd_char_display::init ),
90   trigger_mgr( this )
91 {
92   add_pin( "row-col", &row_col_pin );
93   add_attribute( "row-col", &row_col_pin, "pin" );
94   trigger_mgr.add_watchable_attribute( "row-col" );
95   categorize( "row-col", "watchable" );
96
97   add_pin( "FR", &frame_pin );
98   add_attribute( "FR", &frame_pin, "pin" );
99   trigger_mgr.add_watchable_attribute( "FR" );
100   categorize( "FR", "watchable" );
101
102   add_pin( "init", &init_pin );
103   add_attribute( "init", &init_pin, "pin" );
104   trigger_mgr.add_watchable_attribute( "init" );
105   categorize( "init", "watchable" );
106
107   width = 80;
108   height = 24;
109   add_attribute( "width", &width, "setting" );
110   add_attribute( "height", &height, "setting" );
111
112   add_attribute_virtual ("state-snapshot", this,
113                          & lcd_char_display::save_state,
114                          & lcd_char_display::restore_state);
115   
116 #if HAVE_CURSES_H && HAVE_CURSES_LIBRARY
117   initscr();
118   w = 0;
119 #endif
120 }
121
122 lcd_char_display :: ~lcd_char_display() throw () { 
123 #if HAVE_CURSES_H && HAVE_CURSES_LIBRARY
124   endwin(); 
125 #endif
126 }
127
128 void 
129 lcd_char_display :: init( host_int_4 ) {
130 #if HAVE_CURSES_H && HAVE_CURSES_LIBRARY
131   if( !w ) {
132     w = newwin( height, width, 0, 0 );
133     box( w, 0, 0 );
134   }
135 #endif
136
137   trigger_mgr.check_and_dispatch();
138 }
139
140 void
141 lcd_char_display::set_pixel (host_int_4 val)
142 {
143   int i = val;
144
145   int row = i >> 16;
146   int col = i & 0x0ffff;
147
148 #if HAVE_CURSES_H && HAVE_CURSES_LIBRARY
149   if (!w)
150     return;
151   mvwaddch( w, row, col, '*' );
152 #else
153   std::cout << '[' << row << ',' << col << ']';
154 #endif // HAVE_CURSES_H
155
156   trigger_mgr.check_and_dispatch();
157 }
158
159 void
160 lcd_char_display::new_frame (host_int_4 val)
161 {
162 #if HAVE_CURSES_H && HAVE_CURSES_LIBRARY
163   if (!w)
164     return;
165
166   if (val == 1)
167     werase (w);
168   else
169     wrefresh (w);
170 #else
171   std::cout << std::endl;
172 #endif
173
174   trigger_mgr.check_and_dispatch();
175 }
176
177 string
178 lcd_char_display::save_state ( )
179 {
180     return string ("lcd-char");
181 }
182
183 component::status 
184 lcd_char_display::restore_state(const string& state)
185 {
186    if (state == "lcd-char")
187      return component::ok;
188    return component::bad_value;
189
190
191 static vector<string>
192 DevicesListTypes()
193 {
194   vector<string> types;
195
196   types.push_back( string( "hw-lcd-char-display" ) );
197
198   return types;
199 }
200
201 static component*
202 DevicesCreate(const string& typeName)
203 {
204   if( typeName == "hw-lcd-char-display" )
205     return new lcd_char_display();
206   else
207     return 0;
208 }
209
210 static void
211 DevicesDelete(component* c)
212 {
213   delete dynamic_cast<lcd_char_display*>(c);
214 }
215
216 // static object
217 DLLEXPORT extern const component_library lcd_char_display_component_library;
218
219 const component_library lcd_char_display_component_library =
220 {
221   COMPONENT_LIBRARY_MAGIC,
222   & DevicesListTypes, 
223   & DevicesCreate,
224   & DevicesDelete
225 };