Merge commit '1ef041ff1e390adcd0f97075f1ed52177b7ed3e0'
[osmocom-bb.git] / src / host / osmocon / tpu_debug.c
1 /* Calypso TPU debugger, displays and decodes TPU instruction RAM */
2
3 /* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4  *
5  * All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  */
22
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26
27 #include <osmocore/msgb.h>
28
29 /* TPU disassembler begin */
30
31 static const char *tpu_instr_name[] = {
32         [0]     = "SLEEP",
33         [1]     = "AT",
34         [2]     = "OFFSET",
35         [3]     = "SYNCHRO",
36         [4]     = "MOVE",
37         [5]     = "WAIT",
38         [6]     = "UNDEFINED6",
39         [7]     = "UNDEFINED7",
40 };
41
42 static const char *tpu_addr_name[0x1f] = {
43         [0]     = "TSP_CTLR1",
44         [1]     = "TSP_CTRL2",
45         [4]     = "TSP_TX_1",
46         [3]     = "TSP_TX_2",
47         [2]     = "TSP_TX_3",
48         [5]     = "TSP_TX_4",
49         [6]     = "TSPACT_L",
50         [7]     = "TSPACT_H",
51         [9]     = "TSP_SET1",
52         [0xa]   = "TSP_SET2",
53         [0xb]   = "TSP_SET3",
54         [0x10]  = "DSP_INT_PG",
55         [0x11]  = "GAUGING_EN",
56 };
57
58 static uint8_t tpu_reg_cache[0x1f];
59 static uint16_t tpu_qbit;
60
61 static void tpu_show_instr(uint16_t tpu)
62 {
63         uint16_t instr = tpu >> 13;
64         uint16_t param = tpu & 0x1fff;
65         uint16_t addr, data, bitlen;
66         uint32_t tsp_data;
67
68         tpu_qbit++;
69
70         printf("\t %04u %04x %s ", tpu_qbit, tpu, tpu_instr_name[instr]);
71         switch (instr) {
72         case 0:
73                 tpu_qbit = 0;
74         default:
75                 break;
76         case 1:
77                 tpu_qbit = param;
78                 printf("%u ", param);
79                 break;
80         case 5:
81                 tpu_qbit += param;
82                 printf("%u ", param);
83                 break;
84         case 2:
85         case 3:
86                 printf("%u ", param);
87                 break;
88         case 4:
89                 addr = param & 0x1f;
90                 data = param >> 5;
91                 tpu_reg_cache[addr] = data;
92                 printf("%10s=0x%04x ", tpu_addr_name[addr], data);
93                 switch (addr) {
94                 case 0:
95                         bitlen = (data & 0x1f) + 1;
96                         printf("DEV_IDX=%u, BITLEN=%u ", data >> 5, bitlen);
97                         if (bitlen <= 8) {
98                                 tsp_data = tpu_reg_cache[4];
99                                 printf(" TSP_DATA=0x%02x ", tsp_data);
100                         } else if (bitlen <= 16) {
101                                 tsp_data = tpu_reg_cache[3];
102                                 tsp_data |= tpu_reg_cache[4] << 8;
103                                 printf(" TSP_DATA=0x%04x ", tsp_data);
104                         } else if (bitlen <= 24) {
105                                 tsp_data = tpu_reg_cache[2];
106                                 tsp_data |= tpu_reg_cache[3] << 8;
107                                 tsp_data |= tpu_reg_cache[4] << 16;
108                                 printf(" TSP_DATA=0x%06x ", tsp_data);
109                         } else {
110                                 tsp_data = tpu_reg_cache[5];
111                                 tsp_data |= tpu_reg_cache[2] << 8;
112                                 tsp_data |= tpu_reg_cache[3] << 16;
113                                 tsp_data |= tpu_reg_cache[4] << 24;
114                                 printf(" TSP_DATA=0x%08x ", tsp_data);
115                         }
116                         break;
117                 case 1:
118                         if (data & 0x01)
119                                 printf("READ ");
120                         if (data & 0x02)
121                                 printf("WRITE ");
122                         break;
123                 }
124         }
125         printf("\n");
126 }
127
128 void hdlc_tpudbg_cb(uint8_t dlci, struct msgb *msg)
129 {
130         uint32_t *fn = (uint32_t *) msg->data;
131         uint16_t *tpu;
132
133         printf("TPU FN %u\n", *fn);
134         for (tpu = (uint16_t *) (msg->data + 4); tpu < (uint16_t *) msg->tail; tpu++)
135                 tpu_show_instr(*tpu);
136
137         msgb_free(msg);
138 }