OSDN Git Service

キーボード関連の構造体化
[heavyosecpu/HeavyOSECPU.git] / comlib.c
1 #include "osecpu.h"
2
3 struct ComLib_Str {
4         const unsigned char *p;
5         int bitBuf, bitBufLen;
6         int tmp;
7 };
8
9 int ComLib_getBit(struct ComLib_Str *s)
10 {
11         //ビットを一つずつ取り出す
12         if (s->bitBufLen == 0) {
13                 s->bitBuf = s->p[0] | s->p[1] << 8;
14                 s->p += 2;
15                 s->bitBufLen = 16;
16         }
17         s->bitBufLen--;
18         return (s->bitBuf >> s->bitBufLen) & 1;
19 }
20
21 int ComLib_getTmpBit(struct ComLib_Str *s)
22 {
23         //次のビットをtmpの一番下のビットに押し込んで、その次のビットを返す
24         s->tmp = (s->tmp << 1 | ComLib_getBit(s)) & 0xffff;
25         return ComLib_getBit(s);
26 }
27
28 unsigned char *ComLib_main(const unsigned char *p, unsigned char *q)
29 {
30         //hh4デコーダー?
31         struct ComLib_Str s;
32         int i, dis = 0;
33         dis |= -1;
34         s.p = p;
35         s.bitBufLen = 0;
36         goto l1;
37 l0:
38         *q++ = *s.p++;
39 l1:
40         i = ComLib_getBit(&s);
41         if (i != 0){
42                 //4bit?
43                 goto l0;
44         }
45         s.tmp = 1;
46         do {
47                 i = ComLib_getTmpBit(&s);
48                 if (s.tmp == 0){
49                         goto fin;
50                 }
51         } while (i == 0);
52         if (s.tmp >= 3){
53                 dis = ~((s.tmp - 3) << 8 | *s.p++);
54         }
55         s.tmp &= 0;
56         i = ComLib_getTmpBit(&s);
57         s.tmp = s.tmp << 1 | i;
58         if (s.tmp == 0) {
59                 s.tmp |= 1;
60                 do {
61                         i = ComLib_getTmpBit(&s);
62                 } while (i == 0);
63                 s.tmp += 2;
64         }
65         s.tmp++;
66         if (dis < -0xd00){
67                 s.tmp++;
68         }
69         for (i = 0; i < s.tmp; i++){
70                 q[i] = q[i + dis];
71         }
72         q += s.tmp;
73         goto l1;
74 fin:
75         return q;
76 }