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

Subversion Repositories Aquarius

[/] [Aquarius/] [trunk/] [application/] [shc_pi/] [main.c] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 thorn_aitc
//===============================
2
// Calculate Circular Constant Pi
3
// (1024 figures)
4
//-------------------------------
5
// for VirtexE Evaluation Board
6
// July.20 2003 Ver.1
7
//===============================
8
 
9
//======================================================
10
// Address Map
11
//======================================================
12
// address           sz  wt wd device
13
// 00000000-00001FFF  8K 0  32 ROM (code)
14
// 00002000-00003DFF  6K 0  32 RAM (data)
15
// 00003E00-00003FFF 512 0  32 RAM (stack)
16
//
17
// 00000000-0000FFFF 64K 0  32 RAM (shadow every 16KB)
18
// 00010000-0001FFFF 64K 3  32 RAM (shadow every 16KB)
19
// 00020000-0002FFFF 64K 0  16 RAM (shadow every 16KB)
20
// 00030000-0003FFFF 64K 3  16 RAM (shadow every 16KB)
21
// 00040000-ABCCFFFF (shadow RAM)
22
// ABCD0000-ABCD00FF 256 3  32 PIO (shadow every 4B)
23
// ABCD0100-ABCD01FF 256 3  32 UART(shadow every 4B)
24
// ABCD0200-ABCD02FF 256 3  32 SYS (shadow every 8B)
25
// ABCD0300-FFFBFFFF (shadow RAM)
26
// FFFC0000-FFFCFFFF 64K 0  32 RAM (shadow every 16KB)
27
// FFFD0000-FFFDFFFF 64K 3  32 RAM (shadow every 16KB)
28
// FFFE0000-FFFEFFFF 64K 0  16 RAM (shadow every 16KB)
29
// FFFF0000-FFFFFFFF 64K 3  16 RAM (shadow every 16KB)
30
// 
31
//======================================================
32
// PORT OUTPUT
33
//======================================================
34
// ABCD0000 : reserved
35
//   31    30    29    28    27    26    25    24
36
//    7     6     5     4     3     2     1     0
37
//  -----------------------------------------------
38
// |     |     |     |     |     |     |     |     |
39
//  -----------------------------------------------;
40
// 
41
// ABCD0001 : KEYYO (Key SCAN Y-axis out)
42
//   23    22    21    20    19    18    17    16
43
//    7     6     5     4     3     2     1     0
44
//  -----------------------------------------------
45
// |     |     |     | KY4 | KY3 | KY2 | KY1 | KY0 |
46
//  -----------------------------------------------
47
// 
48
// ABCD0002 : LCDCON (LCD Control Signal)
49
//   15    14    13    12    11    10     9     8
50
//    7     6     5     4     3     2     1     0
51
//  -----------------------------------------------
52
// |     |     |     |     |     |  E  | R/W |  RS |
53
//  -----------------------------------------------
54
// 
55
// ABCD0003 : LCDOUT (Write Data to LCD)
56
//    7     6     5     4     3     2     1     0
57
//    7     6     5     4     3     2     1     0
58
//  -----------------------------------------------
59
// | DW7 | DW6 | DW5 | DW4 | DW3 | DW2 | DW1 | DW0 |
60
//  -----------------------------------------------
61
// 
62
//======================================================
63
// PORT INPUT
64
//======================================================
65
// ABCD0000 : reserved
66
//   31    30    29    28    27    26    25    24
67
//    7     6     5     4     3     2     1     0
68
//  -----------------------------------------------
69
// |     |     |     |     |     |     |     |     |
70
//  -----------------------------------------------;
71
// 
72
// ABCD0001 : KEYXI (Key SCAN X-axis in)
73
//   23    22    21    20    19    18    17    16
74
//    7     6     5     4     3     2     1     0
75
//  -----------------------------------------------
76
// |     |     |     | KX4 | KX3 | KX2 | KX1 | KX0 |
77
//  -----------------------------------------------
78
// 
79
// ABCD0002 : reserved
80
//  15    14    13    12    11    10     9     8
81
//    7     6     5     4     3     2     1     0
82
//  -----------------------------------------------
83
// |     |     |     |     |     |     |     |    |
84
//  -----------------------------------------------
85
// 
86
// ABCD0003 : LCDIN (Read Data from LCD)
87
//    7     6     5     4     3     2     1     0
88
//    7     6     5     4     3     2     1     0
89
//  -----------------------------------------------
90
// | DR7 | DR6 | DR5 | DR4 | DR3 | DR2 | DR1 | DR0 |
91
//  -----------------------------------------------
92
// 
93
//======================================================
94
// UART: SASC (www.opencores.com)
95
// Simple Asynchronous Serial Communication Device
96
//======================================================
97
// ABCD0100 : UARTBG0 Baud Rate Generator Div0 (R/W)
98
//   31    30    29    28    27    26    25    24
99
//    7     6     5     4     3     2     1     0
100
//  -----------------------------------------------
101
// | B07 | B06 | B05 | B04 | B03 | B02 | B01 | B00 |
102
//  -----------------------------------------------
103
// 
104
// ABCD0101 : UARTBG1 Baud Rate Generator Div1 (R/W)
105
//   23    22    21    20    19    18    17    16
106
//    7     6     5     4     3     2     1     0
107
//  -----------------------------------------------
108
// | B17 | B16 | B15 | B14 | B13 | B12 | B11 | B10 |
109
//  -----------------------------------------------
110
// 
111
// ABCD0102 : UARTCON (TXF=full_o, RXE=empty_o) (R only)
112
//   15    14    13    12    11    10     9      8
113
//    7     6     5     4     3     2     1      0
114
//  -----------------------------------------------
115
// |     |     |     |     |     |     | TXF | RXE |
116
//  -----------------------------------------------
117
// 
118
// ABCD0103 : UARTTXD(W only)/UARTRXD(R only)
119
//    7     6     5     4     3     2     1     0
120
//    7     6     5     4     3     2     1     0
121
//  -----------------------------------------------
122
// | TR7 | TR6 | TR5 | TR4 | TR3 | TR2 | TR1 | TR0 |
123
//  -----------------------------------------------
124
// 
125
//======================================================
126
// System Controller
127
// Interrupt and Exception Controller
128
//======================================================
129
// ABCD0200 : INTCTL Interrupt Control (32bit R/W only)
130
//   31    30    29    28    27    26    25    24
131
//  -----------------------------------------------
132
// |E_NMI|E_IRQ|E_CER|E_DER|E_MRS|     |TMRON|BRKON|
133
//  -----------------------------------------------
134
//   23    22    21    20    19    18    17    16
135
//  -----------------------------------------------
136
// |ILVL3|ILVL2|ILVL1|ILVL0|IVEC7|IVEC6|IVEC5|IVEC4|
137
//  ----------------------------------------------- 
138
//   15    14    13    12    11    10     9     8
139
//  -----------------------------------------------
140
// |IVEC3|IVEC2|IVEC1|IVEC0|TMR11|TMR10|TMR09|TMR08|
141
//  -----------------------------------------------  
142
//    7     6     5     4     3     2     1     0
143
//  -----------------------------------------------
144
// |TMR07|TMR06|TMR05|TMR04|TMR03|TMR02|TMR01|TMR00|
145
//  ----------------------------------------------- 
146
//     bit31: E_NMI Emulate NMI Interrupt     (W only)
147
//     bit30: E_IRQ Emulate IRQ Interrupt     (W only)
148
//     bit29: E_CER Emulate CPU Address Error (W only)
149
//     bit28: E_DER Emulate DMA Address Error (W only)
150
//     bit27: E_MRS Emulate Manual Reset      (W only)                               
151
//     bit26: reserved
152
//     bit25: TMRON INTTMR OFF/ON             (0:OFF, 1:ON)
153
//     bit24: BRKON Address Break(NMI) OFF/ON (0:OFF, 1:ON)
154
//     bit23: ILVL3 IRQ ILevel 3
155
//     bit22: ILVL2 IRQ ILevel 2
156
//     bit21: ILVL1 IRQ ILevel 1
157
//     bit20: ILVL0 IRQ ILevel 0
158
//     bit19: IVEC7 IRQ Vector 7
159
//     bit18: IVEC6 IRQ Vector 6
160
//     bit17: IVEC5 IRQ Vector 5
161
//     bit16: IVEC4 IRQ Vector 4
162
//     bit15: IVEC3 IRQ Vector 3
163
//     bit14: IVEC2 IRQ Vector 2
164
//     bit13: IVEC1 IRQ Vector 1
165
//     bit12: IVEC0 IRQ Vector 0
166
//     bit11: TMR11 (12 bit interval timer to generate IRQ)
167
//     ...
168
//     bit 0: TMR00 (12 bit interval timer to generate IRQ)
169
// 
170
// ABCD0204 : BRKADR Break Address (16bit R/W only)
171
//   15    14    13    12    11    10     9     8
172
//  -----------------------------------------------
173
// |ADR15|ADR14|ADR13|ADR12|ADR11|ADR10|ADR09|ADR08|
174
//  -----------------------------------------------  
175
//    7     6     5     4     3     2     1     0
176
//  -----------------------------------------------
177
// |ADR07|ADR06|ADR05|ADR04|ADR03|ADR02|ADR01|ADR00|
178
//  ----------------------------------------------- 
179
//     bit15: ADR15 Break Address15
180
//     ...
181
//     bit00: ADR00 Break Address00
182
// 
183
 
184
#include "common.h"
185
#define  MAXFIGURE ((1024/4)+2) // should be (N*8)+2 to display correctly
186
 
187
//=================
188
// Define Functions
189
//=================
190
void          lcd_message(unsigned char pos, unsigned char *pstr);
191
void          lcd_disp(unsigned char chr);
192
void          lcd_pos(unsigned char pos);
193
void          lcd_ready(void);
194
unsigned char lcd_rd(int);
195
void          lcd_wr(int, unsigned char);
196
void          lcd_init(void);
197
 
198
void          waitNms(unsigned char n);
199
unsigned char key_scan();
200
unsigned char key_wait_on(void);
201
void          key_wait_off(void);
202
 
203
void          longadd(unsigned short *a, unsigned short *b, unsigned short *c);
204
void          longsub(unsigned short *a, unsigned short *b, unsigned short *c);
205
int           longdiv(unsigned short *a, unsigned short b, unsigned short *c);
206
void          calc_pi(void);
207
void          disp_oct4(unsigned short x);
208
void          disp_pi(void);
209
 
210
//-----------------------------------
211
// Work data for Circular Constant Pi
212
//-----------------------------------
213
unsigned short PI[MAXFIGURE];
214
unsigned short T1[MAXFIGURE];
215
unsigned short T2[MAXFIGURE];
216
unsigned short T3[MAXFIGURE];
217
 
218
//=============
219
// Main Routine
220
//=============
221
void main_sh(void)
222
{
223
    unsigned char mes[] = "Calculating.....";
224
 
225
    lcd_init();
226
    lcd_message(0, mes);
227
    calc_pi();
228
    disp_pi();
229
}
230
 
231
//-------------------------------------------------
232
// Calculate the Circular Constant Pi
233
//-------------------------------------------------
234
// (1) Matin's formula
235
// PI/4 =  4*arctan(1/5) -   arctan(1/239)
236
// PI   = 16*arctan(1/5) - 4*arctan(1/239)
237
// Here, arctan(1/p) = 1/p - 1/(3*p^3) + 1/(5*p^5) - 1/(7*p^7) + ...
238
// Then, 
239
// PI =  (16*1/5     - 4*1/239)
240
//     - (16*1/3*5^3 - 4*1/3*239^3)
241
//     + (16*1/5*5^5 - 4*1/5*239^5)
242
//     - (16*1/7*5^7 - 4*1/7*239^7)
243
//     +...
244
//    =  (1/1)*(16*5/(5*5)^1 - 4*239/(239*239)^1)
245
//     - (1/3)*(16*5/(5*5)^2 - 4*239/(239*239)^2)
246
//     + (1/5)*(16*5/(5*5)^3 - 4*239/(239*239)^3)
247
//     - (1/7)*(16*5/(5*5)^4 - 4*239/(239*239)^4)
248
//     +...
249
//-------------------------------------------------
250
// (2) Define Arrays for Long Figure Number
251
// Each element has Octal 4 figures. 
252
//     PI[] : Value to be converged to Pi
253
//     T1[] : (16*  5)/(  5*  5)^n (n=1...)
254
//     T2[] : ( 4*239)/(239*239)^n (n=1...)
255
//     T3[] : (-1)*(n+1)*(1/(2n-1))*(T1[]-T2[])
256
//-------------------------------------------------
257
// (3) Example
258
// n TI[0] TI[1] T2[0] T2[1] T3[0] T3[1] PI[0] PI[1] 
259
// 0 0080  0000  0956  0000  0000  0000  0000  0000
260
// 1 0003  2000  0000  0167  0003  1833  0003  1833 (+)
261
// 2 0000  1280  0000  0000  0000  0426  0003  1407 (-)
262
// 3 0000  0051  0000  0000  0000  0010  0003  1417 (+)
263
// 4 0000  0002  0000  0000  0000  0000  0003  1417 (-)
264
//-------------------------------------------------
265
void calc_pi(void)
266
{
267
    int i;
268
    int converged;
269
    //-----------
270
    // Initialize
271
    //-----------
272
    converged = 0;
273
    for (i=0; i<=MAXFIGURE-1; i++)
274
    {
275
        PI[i] = 0;
276
        T1[i] = 0;
277
        T2[i] = 0;
278
        T3[i] = 0;
279
    }
280
    T1[0] = 16*5;
281
    T2[0] = 4*239;
282
 
283
    //-----------------------------
284
    // repeat until being converged
285
    //-----------------------------
286
    i = 1;
287
    while (converged == 0)
288
    {
289
        //-------------
290
        // Make T1 & T2
291
        //-------------
292
        longdiv(T1,  25, T1);
293
        longdiv(T2, 239, T2);
294
        longdiv(T2, 239, T2);
295
        //--------
296
        // Make T3
297
        //--------
298
        longsub(T1, T2, T3);
299
        converged = longdiv(T3, (unsigned short)(2*i-1), T3);
300
        //if (converged) break;
301
        //-----------------
302
        // Accumulate to PI
303
        //-----------------
304
        if (i%2 != 0)
305
            longadd(PI, T3, PI);
306
        else
307
            longsub(PI, T3, PI);
308
        //------------
309
        // Increment i
310
        //------------
311
        i++;
312
    }
313
}
314
 
315
//---------
316
// Long Add
317
//---------
318
void longadd(unsigned short *a, unsigned short *b, unsigned short *c)
319
{
320
    int i;
321
    unsigned short carry;
322
 
323
    carry = 0;
324
    for (i = MAXFIGURE-1; i >= 0; i--)
325
    {
326
        *(c+i) = *(a+i) + *(b+i) + carry;
327
        if (*(c+i) < 10000)
328
            carry = 0;
329
        else
330
        {
331
            carry = 1;
332
            *(c+i) = *(c+i) - 10000;
333
        }
334
    }
335
}
336
 
337
//---------
338
// Long Sub
339
//---------
340
void longsub(unsigned short *a, unsigned short *b, unsigned short *c)
341
{
342
    int i;
343
    unsigned short borrow;
344
    unsigned short temp;
345
 
346
    borrow = 0;
347
    for (i = MAXFIGURE-1; i >= 0; i--)
348
    {
349
        temp = *(b+i) + borrow;
350
        if (*(a+i) >= temp)
351
        {
352
            *(c+i) = *(a+i) - temp;
353
            borrow = 0;
354
        }
355
        else
356
        {
357
            *(c+i) = 10000 + *(a+i) - temp;
358
            borrow = 1;
359
        }
360
    }
361
}
362
 
363
//---------
364
// Long Div
365
//---------
366
int longdiv(unsigned short *a, unsigned short b, unsigned short *c)
367
{
368
    int i;
369
    int conv;
370
    unsigned long al, bl, cl, dvd, rem;
371
 
372
    conv = 1;
373
    rem = 0;
374
    for (i = 0; i <= MAXFIGURE-1; i++)
375
    {
376
        al = (unsigned long) *(a+i);
377
        bl = (unsigned long) b;
378
        cl = (unsigned long) *(c+i);
379
 
380
        dvd = al + rem;
381
        cl = dvd / b;
382
        rem    = (dvd - cl * bl) * 10000;
383
        if (cl > 0) conv = 0;
384
        *(c+i) = (unsigned short) cl;
385
    }
386
    return conv;
387
}
388
 
389
//-----------
390
// Display Pi
391
//-----------
392
void disp_pi(void)
393
{
394
    unsigned char mes1[] = "Calculating DONE";
395
    unsigned char mes2[] = "Constant Pi = 3.";
396
    int i, j;
397
 
398
    while(1)
399
    {
400
        lcd_message( 0, mes1);
401
        lcd_message(16, mes2);
402
        key_wait_off();
403
        key_wait_on();
404
        // PI[MAXFIGURE-1] has error, so do not display.
405
        for (i = 0; i < (MAXFIGURE - 2) / 8; i++)
406
        {
407
            for (j = 0; j < 8; j++)
408
            {
409
                if (j == 0) lcd_pos(0);
410
                if (j == 4) lcd_pos(16);
411
                disp_oct4(*(PI + i*8 + j + 1));
412
            }
413
            key_wait_off();
414
            key_wait_on();
415
        }
416
    }
417
}
418
 
419
//-------------------
420
// Display Oct 4 digit
421
//-------------------
422
void disp_oct4(unsigned short x)
423
{
424
    unsigned short d;
425
 
426
    d = x / 1000;
427
    x = x - d * 1000;
428
    lcd_disp((char)(d+0x30));
429
    d = x / 100;
430
    x = x - d * 100;
431
    lcd_disp((char)(d+0x30));
432
    d = x / 10;
433
    x = x - d * 10;
434
    lcd_disp((char)(d+0x30));
435
    lcd_disp((char)(x+0x30));
436
}
437
 
438
//****************************************************
439
//*                                                  *
440
//*                 LCD Utilities                    *
441
//*                                                  *
442
//****************************************************
443
 
444
//========================================
445
// Print a Message from Current Position
446
//----------------------------------------
447
//     Input  : pstr = message top address
448
//     Outpur : none
449
//========================================
450
void lcd_message(unsigned char pos, unsigned char *pstr)
451
{
452
    unsigned char *pmessage;
453
 
454
    lcd_pos(pos);
455
    pmessage = pstr;
456
    while (*pmessage) lcd_disp(*pmessage++);
457
}
458
 
459
//==========================================
460
// Display One Character on Current Position
461
//------------------------------------------
462
//     Input  : chr = display charcter
463
//     Output : none
464
//==========================================
465
void lcd_disp(unsigned char chr)
466
{
467
    lcd_ready();
468
    lcd_wr(DATA, chr);
469
}
470
 
471
//================================
472
// Set LCD Display Position
473
//--------------------------------
474
//     Input  : pos = position
475
//                1st line = 00-15
476
//                2nd line = 16-31
477
//     Ouput  : none
478
//================================
479
void lcd_pos(unsigned char pos)
480
{
481
    unsigned char phypos;
482
 
483
    phypos = pos % 32;
484
    if (phypos >= 16)
485
        phypos = (phypos - 16) + 64;
486
    phypos = phypos | 0x80;
487
 
488
    lcd_ready();
489
    lcd_wr(INST, phypos);
490
}
491
 
492
//=====================
493
// Wait until LCD ready
494
//---------------------
495
//     Input  : none
496
//     Output : none 
497
//=====================
498
void lcd_ready(void)
499
{
500
    while ((lcd_rd(INST) & 0x80) == 0x80);
501
}
502
 
503
//=================================
504
// LCD Read
505
//---------------------------------
506
//     Input  : rs (0=Instr,1=Data)
507
//     Output : lcd_rd (Read Data) 
508
//=================================
509
unsigned char lcd_rd(int rs)
510
{
511
    int i;
512
    unsigned char dummy;
513
    unsigned char data;
514
 
515
    PORTO.LCDCON.BYTE = 0x02 | (rs & 0x01);
516
 
517
    // wait 60ns = 2cyc@20MHz
518
    dummy = PORTI.RESERVED_0;
519
    dummy = PORTI.RESERVED_0;
520
 
521
 
522
    PORTO.LCDCON.BYTE = 0x06 | (rs & 0x01);
523
 
524
    // wait 500ns = 10cyc@20MHz
525
    for (i = 0 ; i <= 10 ; i++) dummy = PORTI.RESERVED_0;
526
 
527
    data = PORTI.LCDIN;
528
 
529
    PORTO.LCDCON.BYTE = 0x02 | (rs & 0x01);
530
 
531
    // wait 500ns = 10cyc@20MHz
532
    for (i = 0 ; i <= 10 ; i++) dummy = PORTI.RESERVED_0;
533
 
534
    return(data);
535
}
536
 
537
//=================================
538
// LCD Write
539
//---------------------------------
540
//     Input  : rs (0=Instr,1=Data)
541
//              wd (Write Data)
542
//     Output : none
543
//=================================
544
void lcd_wr(int rs, unsigned char wd)
545
{
546
    int i;
547
    unsigned char dummy;
548
 
549
    PORTO.LCDOUT = wd;
550
 
551
    PORTO.LCDCON.BYTE = 0x00 | (rs & 0x01);
552
 
553
    // wait 60ns = 2cyc@20MHz
554
    dummy = PORTI.RESERVED_0;
555
    dummy = PORTI.RESERVED_0;
556
 
557
    PORTO.LCDCON.BYTE = 0x04 | (rs & 0x01);
558
 
559
    // wait 500ns = 10cyc@20MHz
560
    for (i = 0 ; i <= 10 ; i++) dummy = PORTI.RESERVED_0;
561
 
562
    PORTO.LCDCON.BYTE = 0x00 | (rs & 0x01);
563
 
564
    // wait 500ns = 10cyc@20MHz
565
    for (i = 0 ; i <= 10 ; i++) dummy = PORTI.RESERVED_0;
566
}
567
 
568
//==================
569
// LCD Initialize
570
//------------------
571
//     Input  : none
572
//     Output : none
573
//==================
574
void lcd_init(void)
575
{
576
    int i;
577
    unsigned char dummy;
578
 
579
    lcd_wr(INST, 0x30);
580
 
581
    // wait 4100us=82000cyc@20MHz
582
    for (i = 0 ; i < 82000 ; i++) dummy = PORTI.RESERVED_0;
583
 
584
    lcd_wr(INST, 0x30);
585
 
586
    // wait 100us=2000cyc@20MHz
587
    for (i = 0 ; i < 2000 ; i++) dummy = PORTI.RESERVED_0;
588
 
589
    lcd_wr(INST, 0x30);
590
 
591
    // set function (8bit, 2lines)
592
    lcd_ready();
593
    lcd_wr(INST, 0x38);
594
 
595
    // display (disp on, cursor off, blink off)
596
    lcd_ready();
597
    lcd_wr(INST, 0x0c);
598
 
599
    // clear
600
    lcd_ready();
601
    lcd_wr(INST, 0x01);
602
 
603
    // entry mode (increment, no-shift)
604
    lcd_ready();
605
    lcd_wr(INST, 0x06);
606
}
607
 
608
//****************************************************
609
//*                                                  *
610
//*                 Key Utilities                    *
611
//*                                                  *
612
//****************************************************
613
 
614
//===================================
615
// Wait until Key On
616
//-----------------------------------
617
//     Input  : none
618
//     Output : key = pushed key code
619
//===================================
620
unsigned char key_wait_on(void)
621
{
622
    unsigned char key;
623
 
624
    while((key = key_scan()) == KEYNONE) waitNms(8);
625
 
626
    return(key);
627
}
628
 
629
//===================
630
// Wait until Key Off
631
//-------------------
632
//     Input  : none
633
//     Output : none
634
//===================
635
void key_wait_off(void)
636
{
637
    while(key_scan() != KEYNONE) waitNms(8);
638
}
639
 
640
//===================================
641
// Key Scan
642
//-----------------------------------
643
//     Input  : none
644
//     Output : key = pushed key code
645
//===================================
646
unsigned char key_scan()
647
{
648
    unsigned char keyx4;
649
    unsigned char keyx3;
650
    unsigned char keyx2;
651
    unsigned char keyx1;
652
    unsigned char keyx0;
653
 
654
    PORTO.KEYYO.BYTE = 0xef; // KEYY4
655
    waitNms(1);
656
    keyx4 = PORTI.KEYXI.BYTE & 0x1f;
657
 
658
    PORTO.KEYYO.BYTE = 0xf7; // KEYY3
659
    waitNms(1);
660
    keyx3 = PORTI.KEYXI.BYTE & 0x1f;
661
 
662
    PORTO.KEYYO.BYTE = 0xfb; // KEYY2
663
    waitNms(1);
664
    keyx2 = PORTI.KEYXI.BYTE & 0x1f;
665
 
666
    PORTO.KEYYO.BYTE = 0xfd; // KEYY1
667
    waitNms(1);
668
    keyx1 = PORTI.KEYXI.BYTE & 0x1f;
669
 
670
    PORTO.KEYYO.BYTE = 0xfe; // KEYY0
671
    waitNms(1);
672
    keyx0 = PORTI.KEYXI.BYTE & 0x1f;
673
 
674
    switch(keyx4)
675
    {
676
        case 0x17 : return(KEYPUT);
677
        case 0x1b : return(KEYGET);
678
        case 0x1d : return(KEYRUN);
679
        case 0x1e : return(KEYUTL);
680
    }
681
    switch(keyx3)
682
    {
683
        case 0x0f : return(KEYADR);
684
        case 0x17 : return(KEYF);
685
        case 0x1b : return(KEYE);
686
        case 0x1d : return(KEYD);
687
        case 0x1e : return(KEYC);
688
    }
689
    switch(keyx2)
690
    {
691
        case 0x0f : return(KEYINC);
692
        case 0x17 : return(KEYB);
693
        case 0x1b : return(KEYA);
694
        case 0x1d : return(KEY9);
695
        case 0x1e : return(KEY8);
696
    }
697
    switch(keyx1)
698
    {
699
        case 0x0f : return(KEYDEC);
700
        case 0x17 : return(KEY7);
701
        case 0x1b : return(KEY6);
702
        case 0x1d : return(KEY5);
703
        case 0x1e : return(KEY4);
704
    }
705
    switch(keyx0)
706
    {
707
        case 0x0f : return(KEYDAT);
708
        case 0x17 : return(KEY3);
709
        case 0x1b : return(KEY2);
710
        case 0x1d : return(KEY1);
711
        case 0x1e : return(KEY0);
712
    }
713
    return(KEYNONE);
714
}
715
 
716
//==================
717
// Wait n ms
718
//------------------
719
//     Input  : n ms
720
//     Output : none
721
//==================
722
void waitNms(unsigned char n)
723
{
724
    int  i, j;
725
    unsigned char dummy;
726
 
727
    for (i = 0 ; i < n ; i++)
728
    {
729
        // 1ms = 20000 * 50ns (20MHz)
730
        for (j = 0 ; i <= 10000 ; i++) dummy = PORTI.RESERVED_0;
731
    }
732
}
733
 
734
//===============
735
// End of Program
736
//===============

powered by: WebSVN 2.1.0

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