OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_2_x/] [or1ksim/] [peripheral/] [ps2kbd.c] - Blame information for rev 1308

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 805 markom
/* ps2kbd.c -- Very simple (and limited) PS/2 keyboard simulation
2
   Copyright (C) 2002 Marko Mlinar, markom@opencores.org
3 684 lampret
 
4 805 markom
This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
#include <stdlib.h>
21
#include <stdio.h>
22
#include <string.h>
23
#include "ps2kbd.h"
24
#include "sim-config.h"
25
#include "abstract.h"
26
#include "sched.h"
27 1308 phoenix
#include "pic.h"
28 805 markom
 
29
/* ASCII to scan code conversion table */
30
const static struct {
31
  /* Whether shift must be pressed */
32
  unsigned char shift;
33
  /* Scan code to be generated */
34
  unsigned char code;
35
} scan_table [128] = {
36
/* 0 - 15 */
37
{0, 0x00}, {0, 0x00}, {0, 0x00}, {0, 0x00}, {0, 0x00}, {0, 0x00}, {0, 0x00}, {0, 0x00},
38
{0, 0x0E}, {0, 0x0F}, {0, 0x1C}, {0, 0x00}, {0, 0x00}, {0, 0x00}, {0, 0x00}, {0, 0x00},
39
/* 16 - 31 */
40
{0, 0x00}, {0, 0x00}, {0, 0x00}, {0, 0x00}, {0, 0x00}, {0, 0x00}, {0, 0x00}, {0, 0x00},
41
{0, 0x00}, {0, 0x00}, {0, 0x00}, {0, 0x01}, {0, 0x00}, {0, 0x00}, {0, 0x00}, {0, 0x00},
42
/* 32 - 47 */
43
{0, 0x39}, {1, 0x02}, {1, 0x28}, {1, 0x04}, {1, 0x05}, {1, 0x06}, {1, 0x08}, {0, 0x28},
44
{1, 0x0A}, {1, 0x0B}, {1, 0x09}, {1, 0x0D}, {0, 0x33}, {0, 0x0C}, {0, 0x34}, {0, 0x35},
45
/* 48 - 63 */
46
{0, 0x0B}, {0, 0x02}, {0, 0x03}, {0, 0x04}, {0, 0x05}, {0, 0x06}, {0, 0x07}, {0, 0x08},
47
{0, 0x09}, {0, 0x0A}, {1, 0x27}, {0, 0x27}, {1, 0x33}, {0, 0x0D}, {1, 0x34}, {1, 0x35},
48
/* 64 - 79 */
49
{1, 0x03}, {1, 0x1E}, {1, 0x30}, {1, 0x2E}, {1, 0x20}, {1, 0x12}, {1, 0x21}, {1, 0x22},
50
{1, 0x23}, {1, 0x17}, {1, 0x24}, {1, 0x25}, {1, 0x26}, {1, 0x32}, {1, 0x31}, {1, 0x18},
51
/* 80 - 95 */
52
{1, 0x19}, {1, 0x10}, {1, 0x13}, {1, 0x1F}, {1, 0x14}, {1, 0x16}, {1, 0x2F}, {1, 0x11},
53
{1, 0x2D}, {1, 0x15}, {1, 0x2C}, {0, 0x1A}, {0, 0x2B}, {0, 0x1B}, {1, 0x07}, {1, 0x0C},
54
/* 96 - 111 */
55
{0, 0x29}, {0, 0x1E}, {0, 0x30}, {0, 0x2E}, {0, 0x20}, {0, 0x12}, {0, 0x21}, {0, 0x22},
56
{0, 0x23}, {0, 0x17}, {0, 0x24}, {0, 0x25}, {0, 0x26}, {0, 0x32}, {0, 0x31}, {0, 0x18},
57
/* 112 - 127 */
58
{0, 0x19}, {0, 0x10}, {0, 0x13}, {0, 0x1F}, {0, 0x14}, {0, 0x16}, {0, 0x2F}, {0, 0x11},
59
{0, 0x2D}, {0, 0x15}, {0, 0x2C}, {1, 0x1A}, {1, 0x2B}, {1, 0x1B}, {1, 0x29}, {0, 0x00}
60
};
61
 
62
/* Temporary buffer to store incoming scan codes */
63
static unsigned char kbd_buf[KBD_MAX_BUF] = {0};
64
 
65
/* Number of scan codes in buffer */
66
static unsigned long kbd_buf_count = 0;
67
static unsigned long kbd_buf_head = 0;
68
static unsigned long kbd_buf_tail = 0;
69
 
70
/* Input stream */
71
static FILE *kbd_rxfs = NULL;
72
 
73 684 lampret
/* Controller Command (write into 0x64) */
74
static int kbd_ccmd;
75
 
76
/* Keyboard Command (write into 0x60) */
77
static unsigned char kbd_kcmd;
78
 
79
/* Controller Command Byte */
80
static unsigned char kbd_ccmdbyte;
81
 
82
/* Keyboard response pending */
83
static unsigned long kbd_kresp;
84
 
85 805 markom
/* Keyboard slowdown factor */
86
static long kbd_slowdown;
87
 
88
static void kbd_put (unsigned char c)
89
{
90
  if (kbd_buf_count >= KBD_MAX_BUF) {
91
    fprintf (stderr, "WARNING: Keyboard buffer overflow.\n");
92
  } else {
93
    kbd_buf[kbd_buf_head] = c;
94
    kbd_buf_head = (kbd_buf_head + 1) % KBD_MAX_BUF;
95
    kbd_buf_count++;
96
  }
97
}
98
 
99
/* Decodes ascii code c into multiple scan codes, placed into buf, length is returned */
100
static void scan_decode (unsigned char c)
101
{
102
  /* Do not handle special characters and extended ascii */
103
  if (c >= 128 || !scan_table[c].code)
104
    return;
105
 
106
  /* Make shift? */
107
  if (scan_table[c].shift) kbd_put (0x2a);
108
  /* Make char */
109
  kbd_put (scan_table[c].code);
110
  /* Break char */
111
  kbd_put (scan_table[c].code | 0x80);
112
  /* Break shift? */
113
  if (scan_table[c].shift) kbd_put (0xaa);
114
}
115
 
116
/* Write a register */
117
void kbd_write8 (unsigned long addr, unsigned long value)
118
{
119
  int a = (addr - config.kbd.baseaddr);
120
  switch (a) {
121 684 lampret
    case KBD_CTRL:
122
      kbd_ccmd = value & 0xff;
123
      if (kbd_ccmd == KBD_CCMD_RCB)
124
          kbd_kresp = 0x1;
125
      if (kbd_ccmd == KBD_CCMD_ST1)
126
          kbd_kresp = 0x1;
127
      if (kbd_ccmd == KBD_CCMD_ST2)
128
          kbd_kresp = 0x1;
129
      if (kbd_ccmd == KBD_CCMD_DKI)
130
        kbd_ccmdbyte |= KBD_CCMDBYTE_EN;
131
      if (kbd_ccmd == KBD_CCMD_EKI)
132
        kbd_ccmdbyte &= ~KBD_CCMDBYTE_EN;
133
      if (config.sim.verbose)
134 1308 phoenix
        PRINTF("kbd_write8(%lx) %lx\n", addr, value);
135 805 markom
      break;
136 684 lampret
    case KBD_DATA:
137
      if (kbd_ccmd == KBD_CCMD_WCB) {
138
        kbd_ccmdbyte = value & 0xff;
139
        kbd_ccmd = 0x00;
140
      } else
141
        kbd_kcmd = value & 0xff;
142
      if (kbd_kcmd == KBD_KCMD_DK)
143
        kbd_ccmdbyte |= KBD_CCMDBYTE_EN;
144
      if (kbd_kcmd == KBD_KCMD_EK)
145
        kbd_ccmdbyte &= ~KBD_CCMDBYTE_EN;
146
      kbd_kresp = 0x1;
147
      kbd_ccmd = 0x00;
148
      if (config.sim.verbose)
149 1308 phoenix
        PRINTF("kbd_write8(%lx) %lx\n", addr, value);
150 805 markom
      break;
151
    default:
152 1308 phoenix
      fprintf (stderr, "Write out of keyboard space (0x%08lx)!\n", addr);
153 884 markom
      runtime.sim.cont_run = 0;
154 805 markom
      break;
155
  }
156
}
157
 
158
/* Read a register */
159
unsigned long kbd_read8 (unsigned long addr)
160
{
161
  int a = (addr - config.kbd.baseaddr);
162
  switch (a) {
163 684 lampret
    case KBD_CTRL: {
164
      unsigned long c = 0x0;
165
      if (kbd_kresp || kbd_buf_count)
166
        c |= KBD_STATUS_OBF;
167
      c |= kbd_ccmdbyte & KBD_CCMDBYTE_SYS;
168
      c |= KBD_STATUS_INH;
169
      if (config.sim.verbose)
170 1308 phoenix
        PRINTF("kbd_read8(%lx) %lx\n", addr, c);
171 684 lampret
      return c;
172
    }
173
    case KBD_DATA:
174
      if (kbd_ccmd) {
175
        unsigned long rc;
176
        if (kbd_ccmd == KBD_CCMD_RCB)
177
          rc = kbd_ccmdbyte;
178
        if (kbd_ccmd == KBD_CCMD_ST1)
179
          rc = 0x55;
180
        if (kbd_ccmd == KBD_CCMD_ST2)
181
          rc = 0x00;
182
        kbd_ccmd = 0x00;
183
        kbd_kresp = 0x0;
184
        if (config.sim.verbose)
185 1308 phoenix
          PRINTF("kbd_read8(%lx) %lx\n", addr, rc);
186 684 lampret
        return rc;
187
      }
188
      else if (kbd_kresp) {
189
        unsigned long rc;
190
        if (kbd_kresp == 0x2) {
191
          kbd_kresp = 0x0;
192
          rc = KBD_KRESP_RSTOK;
193
        } else if (kbd_kcmd == KBD_KCMD_RST) {
194
          kbd_kresp = 0x2;
195
          rc = KBD_KRESP_ACK;
196
        } else if (kbd_kcmd == KBD_KCMD_ECHO) {
197
          kbd_kresp = 0x0;
198
          rc = KBD_KRESP_ECHO;
199
        } else {
200
          kbd_kresp = 0x0;
201
          rc = KBD_KRESP_ACK;
202
        }
203
        kbd_kcmd = 0x00;
204
        if (config.sim.verbose)
205 1308 phoenix
          PRINTF("kbd_read8(%lx) %lx\n", addr, rc);
206 684 lampret
        return rc;
207 805 markom
      } else if (kbd_buf_count) {
208
        unsigned long c = kbd_buf[kbd_buf_tail];
209
        kbd_buf_tail = (kbd_buf_tail + 1) % KBD_MAX_BUF;
210
        kbd_buf_count--;
211 684 lampret
        kbd_kresp = 0x0;
212
        if (config.sim.verbose)
213 1308 phoenix
          PRINTF("kbd_read8(%lx) %lx\n", addr, c);
214 805 markom
        return c;
215
      }
216 684 lampret
      kbd_kresp = 0x0;
217
      if (config.sim.verbose)
218 1308 phoenix
        PRINTF("kbd_read8(%lx) fifo empty\n", addr);
219 805 markom
      return 0;
220
    default:
221 1308 phoenix
      fprintf (stderr, "Read out of keyboard space (0x%08lx)!\n", addr);
222 884 markom
      runtime.sim.cont_run = 0;
223 805 markom
      return 0;
224
  }
225
}
226
 
227
 
228
/* Simulation hook. Must be called every couple of clock cycles to simulate incomming data. */
229
void kbd_job(int param)
230
{
231
  int c;
232
  int kbd_int = 0;
233
  /* Check if there is something waiting, and decode it into kdb_buf */
234
  if((c = fgetc(kbd_rxfs)) != EOF) {
235
    scan_decode (c);
236
  }
237
  kbd_int = kbd_kresp || kbd_buf_count;
238
  kbd_int = kbd_kresp || kbd_buf_count ? kbd_ccmdbyte & KBD_CCMDBYTE_INT : 0;
239
  if (config.sim.verbose && kbd_int)
240 1308 phoenix
    PRINTF("Keyboard Interrupt.... kbd_kresp %lx  kbd_buf_count %lx \n",
241
           kbd_kresp, kbd_buf_count);
242 805 markom
  if (kbd_int) report_interrupt(config.kbd.irq);
243 884 markom
  SCHED_ADD(kbd_job, 0, runtime.sim.cycles + kbd_slowdown);
244 805 markom
}
245
 
246
/* Reset all VGAs */
247
void kbd_reset ()
248
{
249
  if (config.kbd.enabled) {
250
    kbd_buf_count = 0;
251
    kbd_buf_head = 0;
252
    kbd_buf_tail = 0;
253 684 lampret
    kbd_kresp = 0x0;
254
    kbd_ccmdbyte = 0x65; /* We reset into default normal operation. */
255 970 simons
    register_memoryarea(config.kbd.baseaddr, KBD_SPACE, 1, 0, kbd_read8, kbd_write8);
256 805 markom
 
257
    if (!(kbd_rxfs = fopen(config.kbd.rxfile, "r"))
258
     && !(kbd_rxfs = fopen(config.kbd.rxfile, "r+"))) {
259
      fprintf (stderr, "WARNING: Keyboard has problems with RX file stream.\n");
260
      config.kbd.enabled = 0;
261
    }
262
    kbd_slowdown = (long) ((config.sim.system_kfreq * 1000.) / KBD_BAUD_RATE);
263
    if (kbd_slowdown <= 0) kbd_slowdown = 1;
264 884 markom
    if (config.kbd.enabled) SCHED_ADD(kbd_job, 0, runtime.sim.cycles + kbd_slowdown);
265 805 markom
  }
266
}
267 684 lampret
 
268 805 markom
 
269 684 lampret
void kbd_info()
270
{
271 997 markom
        PRINTF("kbd_kcmd: %x\n", kbd_kcmd);
272
        PRINTF("kbd_ccmd: %x\n", kbd_ccmd);
273
        PRINTF("kbd_ccmdbyte: %x\n", kbd_ccmdbyte);
274 1308 phoenix
        PRINTF("kbd_kresp: %lx\n", kbd_kresp);
275
        PRINTF("kbd_buf_count: %lx\n", kbd_buf_count);
276 684 lampret
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.