openMSX
I8251.hh
Go to the documentation of this file.
1 // This class implements the Intel 8251 chip (UART)
2 
3 #ifndef I8251_HH
4 #define I8251_HH
5 
6 #include "ClockPin.hh"
7 #include "SerialDataInterface.hh"
8 #include "Schedulable.hh"
9 #include "serialize_meta.hh"
10 #include "openmsx.hh"
11 #include "outer.hh"
12 
13 namespace openmsx {
14 
16 {
17 public:
18  virtual void setRxRDY(bool status, EmuTime::param time) = 0;
19  virtual void setDTR(bool status, EmuTime::param time) = 0;
20  virtual void setRTS(bool status, EmuTime::param time) = 0;
21  [[nodiscard]] virtual bool getDSR(EmuTime::param time) = 0;
22  [[nodiscard]] virtual bool getCTS(EmuTime::param time) = 0; // TODO use this
23  virtual void signal(EmuTime::param time) = 0;
24 
25 protected:
26  I8251Interface() = default;
27  ~I8251Interface() = default;
28 };
29 
30 class I8251 final : public SerialDataInterface
31 {
32 public:
33  I8251(Scheduler& scheduler, I8251Interface& interf, EmuTime::param time);
34 
35  void reset(EmuTime::param time);
36  [[nodiscard]] byte readIO(word port, EmuTime::param time);
37  [[nodiscard]] byte peekIO(word port, EmuTime::param time) const;
38  void writeIO(word port, byte value, EmuTime::param time);
39  [[nodiscard]] ClockPin& getClockPin() { return clock; }
40  [[nodiscard]] bool isRecvReady() const { return recvReady; }
41  [[nodiscard]] bool isRecvEnabled() const;
42 
43  // SerialDataInterface
44  void setDataBits(DataBits bits) override { recvDataBits = bits; }
45  void setStopBits(StopBits bits) override { recvStopBits = bits; }
46  void setParityBit(bool enable, ParityBit parity) override;
47  void recvByte(byte value, EmuTime::param time) override;
48 
49  // Schedulable
50  struct SyncRecv final : Schedulable {
51  friend class I8251;
52  explicit SyncRecv(Scheduler& s) : Schedulable(s) {}
53  void executeUntil(EmuTime::param time) override {
54  auto& i8251 = OUTER(I8251, syncRecv);
55  i8251.execRecv(time);
56  }
58  struct SyncTrans final : Schedulable {
59  friend class I8251;
60  explicit SyncTrans(Scheduler& s) : Schedulable(s) {}
61  void executeUntil(EmuTime::param time) override {
62  auto& i8251 = OUTER(I8251, syncTrans);
63  i8251.execTrans(time);
64  }
66  void execRecv(EmuTime::param time);
67  void execTrans(EmuTime::param time);
68 
69  template<typename Archive>
70  void serialize(Archive& ar, unsigned version);
71 
72  // public for serialize
73  enum CmdFaze {
75  };
76 
77 private:
78  void setMode(byte newMode);
79  void writeCommand(byte value, EmuTime::param time);
80  [[nodiscard]] byte readStatus(EmuTime::param time);
81  [[nodiscard]] byte readTrans(EmuTime::param time);
82  void writeTrans(byte value, EmuTime::param time);
83  void send(byte value, EmuTime::param time);
84 
85 private:
86  I8251Interface& interf;
87  ClockPin clock;
88  unsigned charLength;
89 
90  CmdFaze cmdFaze;
91  SerialDataInterface::DataBits recvDataBits;
92  SerialDataInterface::StopBits recvStopBits;
93  SerialDataInterface::ParityBit recvParityBit;
94  bool recvParityEnabled;
95  byte recvBuf;
96  bool recvReady;
97 
98  byte sendByte;
99  byte sendBuffer;
100 
101  byte status;
102  byte command;
103  byte mode;
104  byte sync1, sync2;
105 };
107 
108 } // namespace openmsx
109 
110 #endif
virtual bool getDSR(EmuTime::param time)=0
virtual void setRxRDY(bool status, EmuTime::param time)=0
virtual void setDTR(bool status, EmuTime::param time)=0
virtual bool getCTS(EmuTime::param time)=0
virtual void setRTS(bool status, EmuTime::param time)=0
virtual void signal(EmuTime::param time)=0
ClockPin & getClockPin()
Definition: I8251.hh:39
byte peekIO(word port, EmuTime::param time) const
Definition: I8251.cc:86
void setStopBits(StopBits bits) override
Definition: I8251.hh:45
bool isRecvReady() const
Definition: I8251.hh:40
void recvByte(byte value, EmuTime::param time) override
Definition: I8251.cc:261
I8251(Scheduler &scheduler, I8251Interface &interf, EmuTime::param time)
Definition: I8251.cc:47
void execRecv(EmuTime::param time)
Definition: I8251.cc:294
void setParityBit(bool enable, ParityBit parity) override
Definition: I8251.cc:255
void writeIO(word port, byte value, EmuTime::param time)
Definition: I8251.cc:96
void reset(EmuTime::param time)
Definition: I8251.cc:55
void setDataBits(DataBits bits) override
Definition: I8251.hh:44
openmsx::I8251::SyncRecv syncRecv
void execTrans(EmuTime::param time)
Definition: I8251.cc:301
byte readIO(word port, EmuTime::param time)
Definition: I8251.cc:77
openmsx::I8251::SyncTrans syncTrans
void serialize(Archive &ar, unsigned version)
Definition: I8251.cc:348
bool isRecvEnabled() const
Definition: I8251.cc:279
Every class that wants to get scheduled at some point must inherit from this class.
Definition: Schedulable.hh:34
This file implemented 3 utility functions:
Definition: Autofire.cc:9
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
SERIALIZE_CLASS_VERSION(CassettePlayer, 2)
#define OUTER(type, member)
Definition: outer.hh:41
void executeUntil(EmuTime::param time) override
When the previously registered syncPoint is reached, this method gets called.
Definition: I8251.hh:53
SyncRecv(Scheduler &s)
Definition: I8251.hh:52
SyncTrans(Scheduler &s)
Definition: I8251.hh:60
void executeUntil(EmuTime::param time) override
When the previously registered syncPoint is reached, this method gets called.
Definition: I8251.hh:61