| 1 |
/* |
| 2 |
Copyright (C) 2005 T. Scott Dattalo |
| 3 |
|
| 4 |
This file is part of gpsim. |
| 5 |
|
| 6 |
gpsim is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2, or (at your option) |
| 9 |
any later version. |
| 10 |
|
| 11 |
gpsim is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with gpsim; see the file COPYING. If not, write to |
| 18 |
the Free Software Foundation, 59 Temple Place - Suite 330, |
| 19 |
Boston, MA 02111-1307, USA. */ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
#ifdef HAVE_GUI |
| 23 |
#include "glcd_100X32_sed1520.h" |
| 24 |
#include "glcd.h" |
| 25 |
#include "sed1520.h" |
| 26 |
|
| 27 |
#include <src/stimuli.h> |
| 28 |
#include <src/ioports.h> |
| 29 |
|
| 30 |
#include <cassert> |
| 31 |
|
| 32 |
//------------------------------------------------------------------------ |
| 33 |
// I/O pins for the graphic LCD |
| 34 |
//------------------------------------------------------------------------ |
| 35 |
// The gLCD_InputPin is the base class for the E1, E2, RW and A0 |
| 36 |
// pins. This class is derived from the IO_bi_directional class, |
| 37 |
// although the pins are never driven as outputs. The setDrivenState |
| 38 |
// method is overridden to capture when this pin is driven. |
| 39 |
|
| 40 |
class gLCD_InputPin : public IO_bi_directional { |
| 41 |
public: |
| 42 |
gLCD_InputPin(gLCD_100X32_SED1520 *, const char *pinName, enPins pin); |
| 43 |
|
| 44 |
// Capture when a node drives this pin. |
| 45 |
virtual void setDrivenState(bool new_dstate); |
| 46 |
|
| 47 |
char CurrentState() |
| 48 |
{ |
| 49 |
return m_cDrivenState; |
| 50 |
} |
| 51 |
|
| 52 |
private: |
| 53 |
// The IO_bi_directional constructor will initialize the direction |
| 54 |
// to be an input. We'll override the update_direction method so that |
| 55 |
// this will never change. |
| 56 |
|
| 57 |
virtual void update_direction(unsigned int, bool /* refresh */ ) |
| 58 |
{ |
| 59 |
// Disallow pin direction changes. |
| 60 |
} |
| 61 |
|
| 62 |
gLCD_100X32_SED1520 *m_pLCD; |
| 63 |
enPins m_pin; |
| 64 |
char m_cDrivenState; |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
//------------------------------------------------------------------------ |
| 69 |
// |
| 70 |
gLCD_InputPin::gLCD_InputPin(gLCD_100X32_SED1520 *pLCD, const char *pinName, enPins pin) |
| 71 |
: IO_bi_directional(pinName), m_pLCD(pLCD), m_pin(pin), m_cDrivenState('Z') |
| 72 |
{ |
| 73 |
assert(m_pLCD); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
void gLCD_InputPin::setDrivenState(bool new_dstate) |
| 78 |
{ |
| 79 |
IO_bi_directional::setDrivenState(new_dstate); |
| 80 |
char cState = getBitChar(); |
| 81 |
|
| 82 |
if (m_cDrivenState != cState) { |
| 83 |
m_cDrivenState = cState; |
| 84 |
m_pLCD->UpdatePinState(m_pin, cState); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
//------------------------------------------------------------------------ |
| 90 |
// |
| 91 |
|
| 92 |
class gLCDSignalControl : public SignalControl { |
| 93 |
public: |
| 94 |
explicit gLCDSignalControl(gLCD_100X32_SED1520 *pLCD) |
| 95 |
: m_pLCD(pLCD) |
| 96 |
{ |
| 97 |
assert(m_pLCD); |
| 98 |
} |
| 99 |
virtual void release() |
| 100 |
{ |
| 101 |
} |
| 102 |
virtual char getState() |
| 103 |
{ |
| 104 |
// return the data bus direction (note the negative true logic. |
| 105 |
return m_pLCD->dataBusDirection() ? '0' : '1'; |
| 106 |
} |
| 107 |
|
| 108 |
private: |
| 109 |
gLCD_100X32_SED1520 *m_pLCD; |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
//------------------------------------------------------------------------ |
| 114 |
bool gLCD_100X32_SED1520::dataBusDirection() |
| 115 |
{ |
| 116 |
// FIXME, if both sed's are driving the data bus, then they'll be in |
| 117 |
// contention. |
| 118 |
return m_sed1->dataBusDirection() || m_sed2->dataBusDirection(); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
//------------------------------------------------------------------------ |
| 123 |
Module *gLCD_100X32_SED1520::construct(const char *_new_name = nullptr) |
| 124 |
{ |
| 125 |
gLCD_100X32_SED1520 *gLCD = new gLCD_100X32_SED1520(_new_name); |
| 126 |
return gLCD; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
//------------------------------------------------------------------------ |
| 131 |
gLCD_100X32_SED1520::gLCD_100X32_SED1520(const char *_new_name) |
| 132 |
: gLCD_Module(_new_name, "SED1520 100X32 Graphics LCD module", 100, 32) |
| 133 |
{ |
| 134 |
m_dataBus = new LcdPortRegister(this, ".data", "LCD Data Port"); |
| 135 |
addSymbol(m_dataBus); |
| 136 |
m_dataBus->setEnableMask(0xff); |
| 137 |
m_A0 = new gLCD_InputPin(this, "a0", enA0); |
| 138 |
m_E1 = new gLCD_InputPin(this, "e1", enE1); |
| 139 |
m_E2 = new gLCD_InputPin(this, "e2", enE2); |
| 140 |
m_RW = new gLCD_InputPin(this, "rw", enRW); |
| 141 |
addSymbol(m_A0); |
| 142 |
addSymbol(m_E1); |
| 143 |
addSymbol(m_E2); |
| 144 |
addSymbol(m_RW); |
| 145 |
m_sed1 = new SED1520(); |
| 146 |
m_sed2 = new SED1520(); |
| 147 |
m_sed1->randomizeRAM(); |
| 148 |
m_sed2->randomizeRAM(); |
| 149 |
create_iopin_map(); |
| 150 |
create_widget(); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
gLCD_100X32_SED1520::~gLCD_100X32_SED1520() |
| 155 |
{ |
| 156 |
delete m_dataBus; |
| 157 |
removeSymbol(m_A0); |
| 158 |
removeSymbol(m_E1); |
| 159 |
removeSymbol(m_E2); |
| 160 |
removeSymbol(m_RW); |
| 161 |
delete m_sed1; |
| 162 |
delete m_sed2; |
| 163 |
gtk_widget_destroy(darea); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
//------------------------------------------------------------------------ |
| 168 |
void gLCD_100X32_SED1520::create_iopin_map() |
| 169 |
{ |
| 170 |
create_pkg(18); |
| 171 |
// Add the individual io pins to the data bus. |
| 172 |
assign_pin(9, m_dataBus->addPin(new IO_bi_directional("d0"), 0)); |
| 173 |
assign_pin(10, m_dataBus->addPin(new IO_bi_directional("d1"), 1)); |
| 174 |
assign_pin(11, m_dataBus->addPin(new IO_bi_directional("d2"), 2)); |
| 175 |
assign_pin(12, m_dataBus->addPin(new IO_bi_directional("d3"), 3)); |
| 176 |
assign_pin(13, m_dataBus->addPin(new IO_bi_directional("d4"), 4)); |
| 177 |
assign_pin(14, m_dataBus->addPin(new IO_bi_directional("d5"), 5)); |
| 178 |
assign_pin(15, m_dataBus->addPin(new IO_bi_directional("d6"), 6)); |
| 179 |
assign_pin(16, m_dataBus->addPin(new IO_bi_directional("d7"), 7)); |
| 180 |
// Provide a SignalControl object that the dataBus port can query |
| 181 |
// to determine which direction to drive the data bus. |
| 182 |
// (See <gpsim/ioports.h> for more documentation on port behavior. |
| 183 |
// But in summary, when an I/O port updates its I/O pins, it will |
| 184 |
// query the pin drive direction via the SignalControl object. ) |
| 185 |
SignalControl *pPortDirectionControl = new gLCDSignalControl(this); |
| 186 |
|
| 187 |
for (int i = 0; i < 8; i++) { |
| 188 |
(*m_dataBus)[i].setControl(pPortDirectionControl); |
| 189 |
} |
| 190 |
|
| 191 |
assign_pin(4, m_A0); |
| 192 |
assign_pin(5, m_RW); |
| 193 |
assign_pin(6, m_E1); |
| 194 |
assign_pin(7, m_E2); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
gboolean gLCD_100X32_SED1520::lcd_expose_event(GtkWidget *widget, |
| 199 |
GdkEventExpose * , |
| 200 |
gLCD_100X32_SED1520 *pLCD) |
| 201 |
{ |
| 202 |
cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(widget)); |
| 203 |
pLCD->m_plcd->clear(cr); |
| 204 |
|
| 205 |
for (unsigned int i = 0; i < pLCD->m_nColumns; ++i) { |
| 206 |
unsigned sedIndex = i < 50 ? i : (i - 50); |
| 207 |
SED1520 *sed = i < 50 ? pLCD->m_sed1 : pLCD->m_sed2; |
| 208 |
|
| 209 |
for (unsigned int j = 0; j < pLCD->m_nRows / 8; ++j) { |
| 210 |
unsigned int row = 8 * j; |
| 211 |
unsigned displayByte = (*sed)[sedIndex + ((j & 3) * 80)]; |
| 212 |
|
| 213 |
for (unsigned int b = 0; b < 8; b++, displayByte >>= 1) |
| 214 |
if (displayByte & 1) { |
| 215 |
pLCD->m_plcd->setPixel(cr, i, row + b); |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
cairo_destroy(cr); |
| 221 |
return TRUE; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
//------------------------------------------------------------------------ |
| 226 |
void gLCD_100X32_SED1520::Update(GtkWidget * ) |
| 227 |
{ |
| 228 |
gtk_widget_queue_draw(darea); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
//------------------------------------------------------------------------ |
| 233 |
void gLCD_100X32_SED1520::create_widget() |
| 234 |
{ |
| 235 |
window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 236 |
gtk_window_set_title(GTK_WINDOW(window), "LCD"); |
| 237 |
GtkWidget *frame = gtk_frame_new("gLCD_100X32"); |
| 238 |
gtk_container_add(GTK_CONTAINER(window), frame); |
| 239 |
darea = gtk_drawing_area_new(); |
| 240 |
gtk_widget_set_size_request(darea, (m_nColumns + 4) * 3, (m_nRows + 4) * 3); |
| 241 |
gtk_container_add(GTK_CONTAINER(frame), darea); |
| 242 |
g_signal_connect(darea, "expose_event", G_CALLBACK(lcd_expose_event), this); |
| 243 |
gtk_widget_set_events(darea, GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK); |
| 244 |
gtk_widget_show_all(window); |
| 245 |
m_plcd = new gLCD(m_nColumns, m_nRows, 3, 3, 1); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
//------------------------------------------------------------------------ |
| 250 |
|
| 251 |
void gLCD_100X32_SED1520::UpdatePinState(enPins pin, char cState) |
| 252 |
{ |
| 253 |
// One of the control lines has changed states. So refresh the |
| 254 |
// sed's with the most current data bus value. |
| 255 |
if (!m_sed1->dataBusDirection()) { |
| 256 |
m_sed1->driveDataBus(m_dataBus->get()); |
| 257 |
} |
| 258 |
|
| 259 |
if (!m_sed2->dataBusDirection()) { |
| 260 |
m_sed2->driveDataBus(m_dataBus->get()); |
| 261 |
} |
| 262 |
|
| 263 |
bool bState = (cState == '1') || (cState == 'W'); |
| 264 |
|
| 265 |
switch (pin) { |
| 266 |
case enA0: |
| 267 |
m_sed1->setA0(bState); |
| 268 |
m_sed2->setA0(bState); |
| 269 |
break; |
| 270 |
|
| 271 |
case enE1: |
| 272 |
m_sed1->setE(bState); |
| 273 |
break; |
| 274 |
|
| 275 |
case enE2: |
| 276 |
m_sed2->setE(bState); |
| 277 |
break; |
| 278 |
|
| 279 |
case enRW: |
| 280 |
m_sed1->setRW(bState); |
| 281 |
m_sed2->setRW(bState); |
| 282 |
break; |
| 283 |
} |
| 284 |
|
| 285 |
// If either one of the sed's is driving, then place that |
| 286 |
// data on to the data bus. |
| 287 |
if (m_sed1->dataBusDirection()) { |
| 288 |
m_dataBus->put(m_sed1->getDataBus()); |
| 289 |
|
| 290 |
} else if (m_sed2->dataBusDirection()) { |
| 291 |
m_dataBus->put(m_sed2->getDataBus()); |
| 292 |
|
| 293 |
} else { |
| 294 |
m_dataBus->updatePort(); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
#endif // HAVE_GUI |