aboutsummaryrefslogtreecommitdiffstats
path: root/simprint.c
blob: 1fe37cbc946b616164f824c74747f5f6de2cbfdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "abcprintd.h"
#include "abcio.h"
#include "z80irq.h"

#define BUF_SIZE 512

static unsigned char output_buf[BUF_SIZE];
static int output_head, output_tail;
static struct abcprint *me;


/* Called to send data abcprint -> abc */
static size_t abcprint_send(void *pvt, const void *buf, size_t count)
{
    const unsigned char *bp = buf;
    size_t sent = 0;

    (void)pvt;

    while (count) {
        int nt = (output_tail + 1) % BUF_SIZE;

        if (nt == output_head)
            break;		/* Output buffer full - data lost */

        output_buf[output_tail] = *bp++;
        output_tail = nt;
	sent++;
	count--;
    }

    return sent;
}

static int abcprint_read(void)
{
    int c;

    if (output_head == output_tail) {
        return -1;
    } else {
        c = output_buf[output_head];
        output_head = (output_head + 1) % BUF_SIZE;
        return c;
    }
}

static bool abcprint_poll(void)
{
    return output_head != output_tail;
}

static struct z80_irq dart_pr_irq;

static void printer_reset(uint8_t sel)
{
    (void)sel;

    if (!me) {
        me = abcprint_init(abcprint_send, NULL);
        if (sel == NO_SELECT)
            z80_register_irq(&dart_pr_irq);
    }
}

static void printer_out(uint8_t sel, uint16_t port, uint8_t value)
{
    (void)sel;

    switch (port) {
    case 0:
	abcprint_recv(me, &value, 1);   /* Data received abc -> abcprint */
        break;

    case 4:
	printer_reset(sel);
        break;

    default:
        break;
    }
}

static uint8_t printer_in(uint8_t sel, uint16_t port)
{
    uint8_t v;

    (void)sel;

    switch (port) {
    case 0:
        v = abcprint_read();
        break;

    case 1:
        v = abcprint_poll() ? 0x40 : 0;
        break;

    default:
        v = -1;
        break;
    }

    return v;
}

static const struct abcbus_dev printer_dev = {
    .out      = printer_out,
    .in       = printer_in,
    .reset    = printer_reset,
    .portmask = 7
};

void printer_init(void)
{
    if (is_abc80())
	register_abcbus_dev(60, &printer_dev);
    else
	printer_reset(NO_SELECT);
}

/* Hardware-like interface via the ABC800 PR: port */
static uint8_t dart_pr_ctl[8];

static struct z80_irq dart_pr_irq = IRQ(IRQ800_DARTA, NULL, NULL, NULL);

void dart_pr_out(uint16_t port, uint8_t v)
{
    uint8_t r;

    switch (port & 1) {
    case 0:                    /* Data port */
        if (dart_pr_ctl[5] & 0x08)
            abcprint_recv(me, &v, 1);
        break;

    case 1:                    /* Control port */
        r = dart_pr_ctl[0] & 7;
        dart_pr_ctl[0] &= ~7;
        dart_pr_ctl[r] = v;
        /* Should to things like supporting interrupts here */
        break;
    }
}

uint8_t dart_pr_in(uint16_t port)
{
    uint8_t r, v = 0;

    switch (port & 1) {
    case 0:                    /* Data port */
        if (dart_pr_ctl[3] & 1)
            v = abcprint_read();
        break;

    case 1:
        r = dart_pr_ctl[0] & 7;
        dart_pr_ctl[0] &= ~7;

        switch (r) {
        case 0:                /* RR0 primary status */
            /*
             * 7 - 0 - No break
             * 6 - 0 - No transmit underrun
             * 5 - 1 - CTS# asserted
             * 4 - x - RI# asserted if 80 columns *on boot* (jumper)
             * 3 - 1 - DCD# asserted
             * 2 - 1 - Transmit buffer empty
             * 1 - 0 - Interrupt not pending
             * 0 - x - Receive character available
             */
            v = 0x2c | (!opts.startup_width40 << 4) |
		(dart_pr_ctl[3] & abcprint_poll());
            break;

        case 1:                /* RR1 Rx special modes */
            /*
             * 5 - 0 - No receiver overrun
             * 4 - 0 - No parity error
             * 0 - 1 - All sent
             */
            v = 0x01;
            break;

        default:
            break;
        }
    }

    return v;
}