1 /*
2  *  KCemu -- The emulator for the KC85 homecomputer series and much more.
3  *  Copyright (C) 1997-2010 Torsten Paul
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License along
16  *  with this program; if not, write to the Free Software Foundation, Inc.,
17  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef __kc_mod_v24_h
21 #define __kc_mod_v24_h
22 
23 #include <string.h>
24 #include <termios.h>
25 
26 #include "kc/ic.h"
27 #include "kc/ports.h"
28 #include "kc/module.h"
29 
30 class ModuleV24 : public ModuleInterface,
31 		  public PortInterface,
32 		  public InterfaceCircuit
33 {
34 private:
35   typedef enum { IO_NONE, IO_FILE, IO_SOCKET, IO_FIFO } io_type_t;
36 
37   enum {
38     A         = 0,
39     B         = 1,
40     CHANNELS  = 2,
41     WR_REGS   = 7,
42     RD_REGS   = 3,
43     INBUF_LEN = 1024,
44   };
45 
46   byte_t _in_buf[INBUF_LEN], *_in_buf_ptr;
47   int _reg[CHANNELS];
48   byte_t _reg_wr[CHANNELS][WR_REGS];
49   byte_t _reg_rd[CHANNELS][RD_REGS];
50   byte_t _data_in[CHANNELS];
51   int    _irq_pending[CHANNELS];
52   int    _irq_active[CHANNELS];
53   int _fd_in[CHANNELS];
54   int _fd_out[CHANNELS];
55   struct termios _tio_old[CHANNELS];
56   struct termios _tio_new[CHANNELS];
57   bool _ok;
58   byte_t _id;
59   byte_t _val;
60   const char *_name;
61   char *_socket_name;
62   int _pid;
63   PortGroup *_portg;
64   io_type_t _io_type;
65 
66 protected:
67   void open_device(void);
68   void close_device(void);
69   bool open_device_serial(int dev, const char *dev_name);
70   bool open_device_socket_or_fifo(io_type_t io_type);
71   void set_signal_handler(int fd, void (*sig_func)(int));
72   static void signal_handler_IO_read(int status);
73   static void signal_handler_IO_recv(int status);
74   void push_data(char *buf, int len);
75   void socket_server(int fd);
76   void fifo_server(int fd);
77 
78 public:
79   ModuleV24(ModuleV24 &tmpl);
80   ModuleV24(const char *name, byte_t id);
81   virtual ~ModuleV24(void);
82 
83   virtual void m_out(word_t addr, byte_t val);
84   virtual ModuleInterface * clone(void);
85   virtual byte_t in_reg(int channel);
86   virtual void out_reg(int channel, byte_t val);
87 
88   /*
89    *  InterfaceCircuit functions
90    *
91    *  FIXME: reset() is inherited twice from both
92    *  FIXME: ModuleInterface and InterfaceCircuit
93    */
94   virtual void reti(void);
irqreq(void)95   virtual void irqreq(void) {}
irqack()96   virtual word_t irqack() { return IRQ_NOT_ACK; }
97   virtual void reset(bool power_on = false);
98 
99   /*
100    *  PortInterface
101    */
102   virtual byte_t in(word_t addr);
103   virtual void out(word_t addr, byte_t val);
104 };
105 
106 #endif /* __kc_mod_v24_h */
107