1 /** AlmostTI: portable TI calcs emulator *********************/
2 /**                                                         **/
3 /**                          TI85.h                         **/
4 /**                                                         **/
5 /** This file contains declarations relevant to the drivers **/
6 /** and TI85 emulation itself. See Z80.h for #defines       **/
7 /** related to Z80 emulation.                               **/
8 /**                                                         **/
9 /** Copyright (C) Marat Fayzullin 1994-2009                 **/
10 /**     You are not allowed to distribute this software     **/
11 /**     commercially. Please, notify me, if you make any    **/
12 /**     changes to this file.                               **/
13 /*************************************************************/
14 #ifndef TI85_H
15 #define TI85_H
16 
17 #include "Z80.h"               /* Z80 CPU emulation          */
18 
19 #define PAGESIZE  0x4000       /* Size of a RAM page         */
20 #define NORAM     0xFF         /* Byte to be returned from   */
21                                /* non-existing addresses     */
22 
23 #define CPU_CLOCK 6000000         /* TI85 Z80 CPU clock (Hz) */
24 #define VIDEO_CLK (CPU_CLOCK/50)  /* Screen refresh period   */
25 #define TIMER_CLK (CPU_CLOCK/200) /* Time clock period       */
26 
27 /** Mode Bits ************************************************/
28 #define ATI_MODEL   0xFF00        /* Calculator model        */
29 #define ATI_TI85    0x0000        /* Emulate TI85 calculator */
30 #define ATI_TI86    0x0100        /* Emulate TI86 calculator */
31 #define ATI_TI82    0x0200        /* Emulate TI82 calculator */
32 #define ATI_TI83    0x0400        /* Emulate TI83 calculator */
33 #define ATI_TI83P   0x0800        /* Emulate TI83+ calculator*/
34 #define ATI_TI83SE  0x1000        /* Emulate TI83+ Silver Ed */
35 #define ATI_TI84    0x2000        /* Emulate TI84 calculator */
36 #define ATI_TI84P   0x2000        /* Emulate TI84+ calculator*/
37 #define ATI_TI84SE  0x4000        /* Emulate TI84+ Silver Ed */
38 
39 #define TI83P_FAMILY ((Mode&ATI_MODEL)>=ATI_TI83P)
40 #define TI83_FAMILY  ((Mode&ATI_MODEL)>=ATI_TI82)
41 #define TI85_FAMILY  ((Mode&ATI_MODEL)<=ATI_TI86)
42 
43 /** I/O Ports ************************************************/
44 #define PORT_LCDBUF   Ports[0]   /* x?AAAAAA                 */
45 #define PORT_KEYPAD   Ports[1]   /* xKKKKKKK                 */
46 #define PORT_CONTRAST Ports[2]   /* xCCCCCCC                 */
47 #define PORT_CONTROL  Ports[3]   /* xxx?LTSK                 */
48 #define PORT_LCDCTRL  Ports[4]   /* xxxWWIIF                 */
49 #define PORT_ROMPAGE  Ports[5]   /* xxxxxPPP or xMxxPPPP     */
50 #define PORT_POWER    Ports[6]   /* ???????P                 */
51 #define PORT_LINK     Ports[7]   /* CCCCDDDD                 */
52 #define PORT_ROMPAGE2 Ports[8]   /* xMxxPPPP in TI86         */
53 #define PORT_STATUS   Ports[15]  /* 0000KTLO                 */
54 #define PORT_ROMPAGE3 Ports[16]  /* xxxxxPPP in TI83+SE      */
55 
56 #define TIMER_IRQ_ON  (PORT_CONTROL&0x04)  /* Timer IRQ on   */
57 #define VIDEO_IRQ_ON  (PORT_CONTROL&0x02)  /* Video IRQ on   */
58 #define ONKEY_IRQ_ON  (PORT_CONTROL&0x01)  /* ON key IRQ on  */
59 #define LCD_ON        (PORT_CONTROL&0x08)  /* LCD display on */
60 #define SLEEP_ON      ((PORT_CONTROL&0x0F)==0x01)
61 
62 #define SCREEN_BUFFER (Page[3]+((int)(PORT_LCDBUF&0x3F)<<8))
63 
64 /** TI82/TI83/TI84 LCD Controller ****************************/
65 #define TI83LCD_BUSY 0x80 /* LCD controller is busy          */
66 #define TI83LCD_8BIT 0x40 /* Using 12x8 scr width (else 16x6)*/
67 #define TI83LCD_ON   0x20 /* LCD screen is on                */
68 #define TI83LCD_DIR  0x03 /* Position increment direction    */
69 
70 typedef struct
71 {
72   byte Buffer[16*64]; /* 120x64 display buffer + 8 pixels */
73   byte Status;        /* LCD_* status buts */
74   byte Col,Row;       /* Cursor position*/
75   byte Delay;         /* 1: Delay next data read */
76   byte Scroll;        /* Vertical scroll value */
77   byte Contrast;      /* Contrast value */
78 } TI83LCD;
79 
80 /** Configuration by Model ***********************************/
81 typedef struct
82 {
83   int         Model;
84   const char *Backdrop;
85   const char *ROMFile;
86   int         ROMSize;
87   const char *RAMFile;
88   int         RAMSize;
89 } TIConfig;
90 
91 /** Keys *****************************************************/
92 #define KBD_SET(K)  KbdStatus[Keys[K][0]]&=~Keys[K][1]
93 #define KBD_RES(K)  KbdStatus[Keys[K][0]]|=Keys[K][1]
94 #define IS_KBD(K)   !(KbdStatus[Keys[K][0]]&Keys[K][1])
95 
96 #define KBD_2ND     0x00
97 #define KBD_ALPHA   0x01
98 #define KBD_XVAR    0x02
99 #define KBD_GRAPH   0x03
100 #define KBD_STAT    0x04   /* TI85 key */
101 #define KBD_TABLE   0x04   /* TI86 key */
102 #define KBD_PRGM    0x05
103 #define KBD_CUSTOM  0x06
104 #define KBD_LOG     0x07
105 #define KBD_DEL     0x08
106 #define KBD_MORE    0x09
107 #define KBD_SIN     0x0A
108 #define KBD_COS     0x0B
109 #define KBD_CLEAR   0x0C
110 #define KBD_ENTER   0x0D
111 #define KBD_TAN     0x0E
112 #define KBD_LN      0x0F
113 #define KBD_EE      0x10
114 #define KBD_SQR     0x11
115 #define KBD_STO     0x12
116 #define KBD_ON      0x13
117 #define KBD_SIGN    0x14
118 #define KBD_F1      0x15
119 #define KBD_F2      0x16
120 #define KBD_F3      0x17
121 #define KBD_F4      0x18
122 #define KBD_F5      0x19
123 #define KBD_EXIT    0x1B
124 #define KBD_LEFT    0x1C
125 #define KBD_RIGHT   0x1D
126 #define KBD_UP      0x1E
127 #define KBD_DOWN    0x1F
128 #define KBD_POWER   '^'
129 #define KBD_LPARENT '('
130 #define KBD_RPARENT ')'
131 #define KBD_DIV     '/'
132 #define KBD_7       '7'
133 #define KBD_8       '8'
134 #define KBD_9       '9'
135 #define KBD_MUL     '*'
136 #define KBD_COMMA   ','
137 #define KBD_4       '4'
138 #define KBD_5       '5'
139 #define KBD_6       '6'
140 #define KBD_MINUS   '-'
141 #define KBD_2       '2'
142 #define KBD_3       '3'
143 #define KBD_4       '4'
144 #define KBD_PLUS    '+'
145 #define KBD_1       '1'
146 #define KBD_0       '0'
147 #define KBD_DOT     '.'
148 
149 /** Variables used to control emulator behavior **************/
150 extern int  Mode;              /* Operating mode bits        */
151 extern byte Verbose;           /* Debugging messages ON/OFF  */
152 extern byte UPeriod;           /* Interrupts / Screen update */
153 extern char *RAMFile;          /* Default state file name    */
154 /*************************************************************/
155 
156 extern Z80 CPU;                /* CPU registers and state    */
157 extern byte *Page[4];          /* 4x16kB address space       */
158 extern byte *ROM,*RAM;         /* RAM and ROM buffers        */
159 extern byte Ports[32];         /* I/O ports                  */
160 extern TI83LCD LCD;            /* TI82/83/84 LCD controller  */
161 extern byte ExitNow;           /* 1: Exit the emulator       */
162 extern byte KbdStatus[8];      /* Keyboard matrix status     */
163 extern const byte Keys[][2];   /* KBD_* to row/column map    */
164 extern const char *ProgDir;    /* Program directory          */
165 extern const char *LinkPeer;   /* Link peer IP address       */
166 extern int LinkPort;           /* Link peer IP port          */
167 extern const TIConfig Config[];/* Config parameters by model */
168 
169 /** StartTI85() **********************************************/
170 /** Allocate memory, load ROM image, initialize hardware,   **/
171 /** CPU and start the emulation. This function returns 0 in **/
172 /** the case of failure.                                    **/
173 /*************************************************************/
174 int StartTI85(char *RAMName);
175 
176 /** TrashTI85() **********************************************/
177 /** Free memory allocated by StartTI85().                   **/
178 /*************************************************************/
179 void TrashTI85(void);
180 
181 /** ResetTI85() **********************************************/
182 /** Reset TI85 hardware to new operating modes. Returns new **/
183 /** modes, possibly not the same as NewMode.                **/
184 /*************************************************************/
185 int ResetTI85(int NewMode);
186 
187 /** SaveSTA() ************************************************/
188 /** Save emulation state to a .STA file.                    **/
189 /*************************************************************/
190 int SaveSTA(const char *FileName);
191 
192 /** LoadSTA() ************************************************/
193 /** Load emulation state from a .STA file.                  **/
194 /*************************************************************/
195 int LoadSTA(const char *FileName);
196 
197 /** InitMachine() ********************************************/
198 /** Allocate resources needed by the machine-dependent code.**/
199 /************************************ TO BE WRITTEN BY USER **/
200 int InitMachine(void);
201 
202 /** TrashMachine() *******************************************/
203 /** Deallocate all resources taken by InitMachine().        **/
204 /************************************ TO BE WRITTEN BY USER **/
205 void TrashMachine(void);
206 
207 /** RefreshScreen() ******************************************/
208 /** Refresh picture on the screen.                          **/
209 /************************************ TO BE WRITTEN BY USER **/
210 void RefreshScreen(void);
211 
212 /** SetColor() ***********************************************/
213 /** Set color N (0/1) to (R,G,B).                           **/
214 /************************************ TO BE WRITTEN BY USER **/
215 void SetColor(byte N,byte R,byte G,byte B);
216 
217 /** Keypad() *************************************************/
218 /** Poll the keyboard. Returns 1 if KBD_ON pressed, else 0. **/
219 /************************************ TO BE WRITTEN BY USER **/
220 byte Keypad(void);
221 
222 /** ShowBackdrop() *******************************************/
223 /** Show backdrop image with calculator faceplate.          **/
224 /************************************ TO BE WRITTEN BY USER **/
225 int ShowBackdrop(const char *FileName);
226 
227 #endif /* TI85_H */
228