📄 slavio_serial.c
字号:
/* * QEMU Sparc SLAVIO serial port emulation * * Copyright (c) 2003-2005 Fabrice Bellard * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */#include "vl.h"/* debug serial *///#define DEBUG_SERIAL/* debug keyboard *///#define DEBUG_KBD/* debug mouse *///#define DEBUG_MOUSE/* * This is the serial port, mouse and keyboard part of chip STP2001 * (Slave I/O), also produced as NCR89C105. See * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt * * The serial ports implement full AMD AM8530 or Zilog Z8530 chips, * mouse and keyboard ports don't implement all functions and they are * only asynchronous. There is no DMA. * *//* * Modifications: * 2006-Aug-10 Igor Kovalenko : Renamed KBDQueue to SERIOQueue, implemented * serial mouse queue. * Implemented serial mouse protocol. */#ifdef DEBUG_SERIAL#define SER_DPRINTF(fmt, args...) \do { printf("SER: " fmt , ##args); } while (0)#define pic_set_irq(irq, level) \do { printf("SER: set_irq(%d): %d\n", (irq), (level)); pic_set_irq((irq),(level));} while (0)#else#define SER_DPRINTF(fmt, args...)#endif#ifdef DEBUG_KBD#define KBD_DPRINTF(fmt, args...) \do { printf("KBD: " fmt , ##args); } while (0)#else#define KBD_DPRINTF(fmt, args...)#endif#ifdef DEBUG_MOUSE#define MS_DPRINTF(fmt, args...) \do { printf("MSC: " fmt , ##args); } while (0)#else#define MS_DPRINTF(fmt, args...)#endiftypedef enum { chn_a, chn_b,} chn_id_t;#define CHN_C(s) ((s)->chn == chn_b? 'b' : 'a')typedef enum { ser, kbd, mouse,} chn_type_t;#define SERIO_QUEUE_SIZE 256typedef struct { uint8_t data[SERIO_QUEUE_SIZE]; int rptr, wptr, count;} SERIOQueue;typedef struct ChannelState { int irq; int reg; int rxint, txint, rxint_under_svc, txint_under_svc; chn_id_t chn; // this channel, A (base+4) or B (base+0) chn_type_t type; struct ChannelState *otherchn; uint8_t rx, tx, wregs[16], rregs[16]; SERIOQueue queue; CharDriverState *chr;} ChannelState;struct SerialState { struct ChannelState chn[2];};#define SERIAL_MAXADDR 7static void handle_kbd_command(ChannelState *s, int val);static int serial_can_receive(void *opaque);static void serial_receive_byte(ChannelState *s, int ch);static inline void set_txint(ChannelState *s);static void put_queue(void *opaque, int b){ ChannelState *s = opaque; SERIOQueue *q = &s->queue; SER_DPRINTF("channel %c put: 0x%02x\n", CHN_C(s), b); if (q->count >= SERIO_QUEUE_SIZE) return; q->data[q->wptr] = b; if (++q->wptr == SERIO_QUEUE_SIZE) q->wptr = 0; q->count++; serial_receive_byte(s, 0);}static uint32_t get_queue(void *opaque){ ChannelState *s = opaque; SERIOQueue *q = &s->queue; int val; if (q->count == 0) { return 0; } else { val = q->data[q->rptr]; if (++q->rptr == SERIO_QUEUE_SIZE) q->rptr = 0; q->count--; } KBD_DPRINTF("channel %c get 0x%02x\n", CHN_C(s), val); if (q->count > 0) serial_receive_byte(s, 0); return val;}static int slavio_serial_update_irq_chn(ChannelState *s){ if ((s->wregs[1] & 1) && // interrupts enabled (((s->wregs[1] & 2) && s->txint == 1) || // tx ints enabled, pending ((((s->wregs[1] & 0x18) == 8) || ((s->wregs[1] & 0x18) == 0x10)) && s->rxint == 1) || // rx ints enabled, pending ((s->wregs[15] & 0x80) && (s->rregs[0] & 0x80)))) { // break int e&p return 1; } return 0;}static void slavio_serial_update_irq(ChannelState *s){ int irq; irq = slavio_serial_update_irq_chn(s); irq |= slavio_serial_update_irq_chn(s->otherchn); pic_set_irq(s->irq, irq);}static void slavio_serial_reset_chn(ChannelState *s){ int i; s->reg = 0; for (i = 0; i < SERIAL_MAXADDR; i++) { s->rregs[i] = 0; s->wregs[i] = 0; } s->wregs[4] = 4; s->wregs[9] = 0xc0; s->wregs[11] = 8; s->wregs[14] = 0x30; s->wregs[15] = 0xf8; s->rregs[0] = 0x44; s->rregs[1] = 6; s->rx = s->tx = 0; s->rxint = s->txint = 0; s->rxint_under_svc = s->txint_under_svc = 0;}static void slavio_serial_reset(void *opaque){ SerialState *s = opaque; slavio_serial_reset_chn(&s->chn[0]); slavio_serial_reset_chn(&s->chn[1]);}static inline void clr_rxint(ChannelState *s){ s->rxint = 0; s->rxint_under_svc = 0; if (s->chn == chn_a) s->rregs[3] &= ~0x20; else s->otherchn->rregs[3] &= ~4; if (s->txint) set_txint(s); else s->rregs[2] = 6; slavio_serial_update_irq(s);}static inline void set_rxint(ChannelState *s){ s->rxint = 1; if (!s->txint_under_svc) { s->rxint_under_svc = 1; if (s->chn == chn_a) s->rregs[3] |= 0x20; else s->otherchn->rregs[3] |= 4; s->rregs[2] = 4; slavio_serial_update_irq(s); }}static inline void clr_txint(ChannelState *s){ s->txint = 0; s->txint_under_svc = 0; if (s->chn == chn_a) s->rregs[3] &= ~0x10; else s->otherchn->rregs[3] &= ~2; if (s->rxint) set_rxint(s); else s->rregs[2] = 6; slavio_serial_update_irq(s);}static inline void set_txint(ChannelState *s){ s->txint = 1; if (!s->rxint_under_svc) { s->txint_under_svc = 1; if (s->chn == chn_a) s->rregs[3] |= 0x10; else s->otherchn->rregs[3] |= 2; s->rregs[2] = 0; slavio_serial_update_irq(s); }}static void slavio_serial_update_parameters(ChannelState *s){ int speed, parity, data_bits, stop_bits; QEMUSerialSetParams ssp; if (!s->chr || s->type != ser) return; if (s->wregs[4] & 1) { if (s->wregs[4] & 2) parity = 'E'; else parity = 'O'; } else { parity = 'N'; } if ((s->wregs[4] & 0x0c) == 0x0c) stop_bits = 2; else stop_bits = 1; switch (s->wregs[5] & 0x60) { case 0x00: data_bits = 5; break; case 0x20: data_bits = 7; break; case 0x40: data_bits = 6; break; default: case 0x60: data_bits = 8; break; } speed = 2457600 / ((s->wregs[12] | (s->wregs[13] << 8)) + 2); switch (s->wregs[4] & 0xc0) { case 0x00: break; case 0x40: speed /= 16; break; case 0x80: speed /= 32; break; default: case 0xc0: speed /= 64; break; } ssp.speed = speed; ssp.parity = parity; ssp.data_bits = data_bits; ssp.stop_bits = stop_bits; SER_DPRINTF("channel %c: speed=%d parity=%c data=%d stop=%d\n", CHN_C(s), speed, parity, data_bits, stop_bits); qemu_chr_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp);}static void slavio_serial_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val){ SerialState *ser = opaque; ChannelState *s; uint32_t saddr; int newreg, channel; val &= 0xff; saddr = (addr & 3) >> 1; channel = (addr & SERIAL_MAXADDR) >> 2; s = &ser->chn[channel]; switch (saddr) { case 0: SER_DPRINTF("Write channel %c, reg[%d] = %2.2x\n", CHN_C(s), s->reg, val & 0xff); newreg = 0; switch (s->reg) { case 0: newreg = val & 7; val &= 0x38; switch (val) { case 8: newreg |= 0x8; break; case 0x28: clr_txint(s); break; case 0x38: if (s->rxint_under_svc) clr_rxint(s); else if (s->txint_under_svc)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -