File: config.c

package info (click to toggle)
picasm 1.5-2.1
  • links: PTS
  • area: non-free
  • in suites: hamm, slink
  • size: 168 kB
  • ctags: 342
  • sloc: ansic: 3,440; asm: 150; makefile: 33
file content (229 lines) | stat: -rw-r--r-- 5,629 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * picasm -- config.c
 *
 * handles configuration fuse setting with the CONFIG-directive
 *
 */

#include <stdio.h>
#include <string.h>

#include "picasm.h"

/*
 * Check Yes/No (or Disable(d)/Enable(d) or On/Off) strings for CONFIG
 *
 * returns: 0=no, 1=yes, -1=error
 *
 */
static int
config_yes_no(char *s)
{
  if(strcasecmp(s, "yes") == 0 || strcasecmp(s, "on") == 0 ||
     strncasecmp(s, "enable", 6) == 0)
    return 1;
  else if(strcasecmp(s, "no") == 0 || strcasecmp(s, "off") == 0 ||
	  strncasecmp(s, "disable", 7) == 0)
    return 0;
  else
    return -1; /* error */
}

/*
 * selstr consists of NUL-terminated strings with two NULs in the end
 * If str matches one of the components of selstr, the index of
 * that component (starting from zero) is returned,
 * if no match is found, -1 is returned.
 */
static int
strsel(char *selstr, char *str)
{
  int sel = 0;

  while(*selstr != '\0') {
    if(strcasecmp(selstr, str) == 0)
      return sel;

    selstr += strlen(selstr)+1;
    sel++;
  }

  return -1;
}

/*
 * Parse CONFIG-directive and set the value
 * of the configuration fuse register
 */
void
parse_config(void)
{
  static char symname[256];
  int t;

  for(;;) {
    if(token_type != TOK_IDENTIFIER) {
cfg_error:
      error(1, "CONFIG syntax error");
      return;
    }
    strcpy(symname, token_string);
    get_token();

    /* hmm... this is a little kludge, but as
       the tokenizer now makes 'local id's from
       the valid config strings, this must be used... */
    if(token_type != TOK_LOCAL_ID) {
      if(token_type != TOK_EQUAL) {
	error(1, "'=' expected");
	return;
      }
      get_token();
      if(token_type != TOK_IDENTIFIER)
	goto cfg_error;
    }

    if(instr_set == PIC12BIT) {
      switch(strsel("CP\0WDT\0OSC\0", symname)) {
        case 0: /* CP -- code protect */
	  if((t = config_yes_no(token_string)) < 0)
	    goto cfg_error;

	  config_fuses = (config_fuses & 0xff7) | (t ? 0 : 0x8);
	  break;

	case 1: /* WDT -- watchdog timer */
	  if((t = config_yes_no(token_string)) < 0)
	    goto cfg_error;

	  config_fuses = (config_fuses & 0xffb) | (t ? 4 : 0);
	  break;

	case 2: /* OSC -- oscillator select */
	  if((t = strsel("LP\0XT\0HS\0RC\0", token_string)) < 0)
	    goto cfg_error;

	  config_fuses = (config_fuses & 0xffc) | t;
	  break;

	default:
	  goto cfg_error;
      }
    } else if(strcmp(pic_type->name, "14000") == 0) {
      /*
       * PIC14000 config flags are a special case,
       * as they are quite different from the other 14-bit PICs
       */
      switch(strsel("CPC\0CPU\0CPP\0PWRT\0WDT\0OSC\0", symname)) {
	case 0: /* CPC -- calibr. space code protection (bit #7, 0==enabled) */
	  if((t = config_yes_no(token_string)) < 0)
	    goto cfg_error;

	  config_fuses = ((config_fuses & 0x3f7f) | (t ? 0 : 0x80));
	  break;

        case 1: /* CPU -- user space code protection (bit #5, 0==enabled) */
	  if((t = config_yes_no(token_string)) < 0)
	    goto cfg_error;

	  config_fuses = ((config_fuses & 0x3fdf) | (t ? 0 : 0x20));
	  break;

	case 2: /* CPP -- program space code protection (bit #4, 0==enabled) */
	  if((t = config_yes_no(token_string)) < 0)
	    goto cfg_error;

	  config_fuses = ((config_fuses & 0x3fef) | (t ? 0 : 0x10));
	  break;

	case 3: /* PWRT -- powerup timer (bit #3, 0==enabled) */
	  if((t = config_yes_no(token_string)) < 0)
	    goto cfg_error;

	  config_fuses = (config_fuses & 0x3ff7) | (t ? 0 : 8);
	  break;

	case 4: /* WDT -- watchdog timer (bit #2, 1==enabled) */
	  if((t = config_yes_no(token_string)) < 0)
	    goto cfg_error;

	  config_fuses = (config_fuses & 0x3ffb) | (t ? 4 : 0);
	  break;

	case 5: /* OSC -- oscillator select (bit #0, 0==HS, 1==IN) */
	  if((t = strsel("HS\0IN\0", token_string)) < 0)
	    goto cfg_error;

	  config_fuses = (config_fuses & 0x3ffe) | t;
	  break;

	default:
	  goto cfg_error;
      }
      /* end of PIC14000 config handling */
    } else { /* normal 14bit PIC */
      switch(strsel("CP\0PWRT\0WDT\0BOD\0OSC\0", symname)) {
        case 0: /* CP -- code protect */
	  if((t = config_yes_no(token_string)) < 0)
	    goto cfg_error;

	  if(pic_type->instr_flags & PIC_CP2) {
	    /* two code protect bits (16C64/65/74) */
	    /* (partial protection currently not supported) */
	    config_fuses = (config_fuses & 0x3fcf) | (t ? 0 : 0x30);
	  } else if(pic_type->instr_flags & PIC_CP3) {
	    /* replicated code-protect bits (PIC16C62x) */
	    /* (partial protection currently not supported) */
	    config_fuses = (config_fuses & 0x00cf) | (t ? 0 : 0x3f30);
	  } else {
	    /* one code protect bit (16C61/84/71) */
	    config_fuses = (config_fuses & 0x3fef) | (t ? 0 : 0x10);
	  }
	  break;

	case 1: /* PWRT -- powerup timer */
	  if((t = config_yes_no(token_string)) < 0)
	    goto cfg_error;

	  if(pic_type->instr_flags & PIC_PWRT_INV)
	    t = !t;

	  config_fuses = (config_fuses & 0x3ff7) | (t ? 8 : 0);
	  break;

	case 2: /* WDT -- watchdog timer */
	  if((t = config_yes_no(token_string)) < 0)
	    goto cfg_error;

	  config_fuses = (config_fuses & 0x3ffb) | (t ? 4 : 0);
	  break;

	case 3: /* BOD -- brown-out detect */
	  if((pic_type->instr_flags & PIC_BOD) == 0)
	    goto cfg_error;

	  if((t = config_yes_no(token_string)) < 0)
	    goto cfg_error;

	  config_fuses = (config_fuses & 0x3fbf) | (t ? 0x40 : 0);
	  break;

	case 4: /* OSC -- oscillator select */
	  if((t = strsel("LP\0XT\0HS\0RC\0", token_string)) < 0)
	    goto cfg_error;

	  config_fuses = (config_fuses & 0x3ffc) | t;
	  break;

	default:
	  goto cfg_error;
      }
    }

    get_token();
    if(token_type != TOK_COMMA)
      break;

    get_token();
  }
}