1 #ifndef SPI_H
2 #define SPI_H
3 
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7 
8 #include "port.h"
9 
10 #include <stdint.h>
11 #include <stdbool.h>
12 #include <stdio.h>
13 
14 #define SPI_RED 0
15 #define SPI_GREEN 1
16 #define SPI_BLUE 2
17 #define SPI_ALPHA 3
18 #define SPI_NUM_ROWS 320
19 #define SPI_LAST_ROW 319
20 #define SPI_NUM_COLS 240
21 #define SPI_LAST_COL 239
22 
23 enum spi_mode {
24     SPI_MODE_SLEEP   = 1 << 0,
25     SPI_MODE_OFF     = 1 << 1,
26     SPI_MODE_BLANK   = 1 << 2,
27     SPI_MODE_PARTIAL = 1 << 3,
28     SPI_MODE_INVERT  = 1 << 4,
29     SPI_MODE_IDLE    = 1 << 5,
30     SPI_MODE_SCROLL  = 1 << 6,
31     SPI_MODE_IGNORE  = 1 << 7
32 };
33 
34 enum spi_mac {
35     SPI_MAC_HRO = 1 << 2,
36     SPI_MAC_BGR = 1 << 3,
37     SPI_MAC_VRO = 1 << 4,
38     SPI_MAC_RCX = 1 << 5,
39     SPI_MAC_CAO = 1 << 6,
40     SPI_MAC_RAO = 1 << 7
41 };
42 
43 enum spi_ic {
44     SPI_IC_CTRL_SYNC   = 1 << 0,
45     SPI_IC_CTRL_DATA   = 1 << 4,
46     SPI_IC_GRAM_BYPASS = 1 << 7
47 };
48 
49 typedef struct spi_state {
50     uint32_t param;
51     uint16_t fifo, row, dstRow, srcRow;
52     uint8_t cmd, col, colDir;
53 
54     uint32_t scanLine, rowReg, colReg;
55     uint16_t rowStart, rowEnd, colStart, colEnd;
56     uint16_t partialStart, partialEnd, topArea, scrollArea, bottomArea, scrollStart;
57     uint8_t mode, ifBpp, ifCtl, ifRed, ifGreen, ifBlue, mac, gamma;
58     uint8_t lut[128], frame[320][240][3], display[240][320][4];
59 
60     bool tear;
61     uint16_t tearLine;
62     uint8_t gammaCorrection[2][16];
63 } spi_state_t;
64 
65 extern spi_state_t spi;
66 
67 eZ80portrange_t init_spi(void);
68 void spi_reset(void);
69 bool spi_hsync(void);
70 bool spi_vsync(void);
71 bool spi_refresh_pixel(void);
72 void spi_update_pixel_18bpp(uint8_t r, uint8_t g, uint8_t b);
73 void spi_update_pixel_16bpp(uint8_t r, uint8_t g, uint8_t b);
74 void spi_update_pixel_12bpp(uint8_t r, uint8_t g, uint8_t b);
75 bool spi_restore(FILE *image);
76 bool spi_save(FILE *image);
77 
78 #ifdef __cplusplus
79 }
80 #endif
81 
82 #endif
83