; CC7A Ver 1.0D, Copyright (c) B Knudsen Data ; C compiler for the PIC18 microcontrollers ; ************ 24. Feb 2011 12:28 ************* processor 17C766 radix DEC WREG EQU 0x0A TBLPTR EQU 0x0D Carry EQU 0 i EQU 0x20 t EQU 0x21 p EQU 0x23 si EQU 0x24 strID EQU 0x23 a EQU 0x25 adr EQU 0x20 i_2 EQU 0x22 ci EQU 0x25 GOTO main ; FILE 17\demo-rom.c ;// DATA STORED IN PROGRAM MEMORY ; ;#pragma chip PIC17C766 // select device ; ; ;// THE 'const' TYPE MODIFIER IS RECOMMENDED FOR ALL ROM DATA ;const int16 tx[10] = { 1, 2, 1000, -34, -30000, 2, 100, 23, 0, 0}; ; ;const char str[] = "String 1"; ; ;const struct { ; const char *str; ;} txtp[] = { ; "A table of pointer to strings", ; "Monday", "Tuesday", ; "MyString", ; "MyString", // equal strings and substrings are merged to save space ; "String", // merged with substring ;}; ; ; ;// NOTE: const data that are not read are NOT stored! ;const char notAccessed[] = "This data is removed because it is not read"; ; ;void sub(uns8 i) ;{ sub MOVWF i ; // reading the 'const' table ; int16 t = tx[i]; BCF 0x04,Carry RLCF i,W CALL _const1 MOVWF t INCF ci,W CALL _const1 MOVWF t+1 ; ; const char *p = str; MOVLW 20 MOVWF p ; i = *p; CALL _const1 MOVWF i ; p = "Hello world"; MOVLW 89 MOVWF p ; i = *p; CALL _const1 MOVWF i ; ; char si; ; p = txtp[si].str; MOVLW 29 ADDWF si,W CALL _const1 MOVWF p ; i = *p; CALL _const1 MOVWF i ;} RETURN ; ; ; ; ;// ********************************************************************** ;// ********************************************************************** ;// ********************************************************************** ; ;// DATA CAN BE STORED DIRECTLY IN PROGRAM MEMORY ;// NOTE: This is for special needs, when 'const' tables is not sufficient ; ;#define CDATA_START 0x600 ;#pragma cdata[CDATA_START] // start of cdata block ; ;// STORE 12 bits data ;#pragma cdata[] = 0xFFF, 0x200, 0x100 ; ;// CONSTANT EXPRESSIONS ARE EVALUATED BEFORE STORING ;#pragma cdata[] = 0x100, (10<<2) + 346,\ ; 10, 456, 1000 ; ; ;#define D8(l,h) l + h*128 ;#define D16(x) x%0x100, x/0x100 ; ;// STORE 16 BITS DATA ;#pragma cdata[] = D8(10,20), D16(23453) ; ;// AUTOMATIC ADDRESS CALCULATION ;#pragma cdata.ID0 = 0x10, 200+300 ;#pragma cdata.ID1 = "Hello world\0" ;#pragma cdata.ID2 = "Another string\r\n" "merged" ; ;// STORE A TABLE OF 16 BIT ADDRESSES TO ROM CODE ;#pragma cdata.ID_TABLE = ID0, ID1, ID2 // store addresses ; ;#pragma cdata.CDATA_END // end of cdata block ; ; ;#pragma origin CDATA_END // program code follow right after ORG 0x0621 ; ; ;void write(uns16 strID) ;{ write ; char a; ; TBLPTR = strID; MOVFP strID,TBLPTR MOVFP strID+1,TBLPTR+1 ; readLU(); // table read-LSB-update TABLRD 0,0,WREG ; a = readL(); // read LSB of LATCH TLRD 0,a ; // process LSB ; a = readH(); // read MSB of LATCH TLRD 1,a ; // process MSB ;} RETURN ; ; ;// ********************************************************************** ;// ********************************************************************** ;// ********************************************************************** ; ;/* NOTE: functions using labels in the directly stored data array must be ; defined after the data definitions */ ; ;void pointers( void) ;{ pointers ; write(ID1); MOVLW 13 MOVWF strID MOVLW 6 MOVWF strID+1 CALL write ; write(ID2); MOVLW 19 MOVWF strID MOVLW 6 MOVWF strID+1 CALL write ; ; uns16 adr; ; char i = 2; // index of ID2 MOVLW 2 MOVWF i_2 ; TBLPTR = i * 2; CLRF TBLPTR+1,1 BCF 0x04,Carry RLCF i_2,W MOVWF TBLPTR ; TBLPTR += ID_TABLE; MOVLW 30 ADDWF TBLPTR,1 MOVLW 6 ADDWFC TBLPTR+1,1 ; readLU(); // table read-LSB-update TABLRD 0,0,WREG ; adr.low8 = readL(); // read LSB of LATCH TLRD 0,adr ; adr.high8 = readH(); // read MSB of LATCH TLRD 1,adr+1 ; write( adr); MOVFP adr,WREG MOVWF strID MOVFP adr+1,WREG MOVWF strID+1 GOTO write ;} ; ; ;page0 void main(void) ;{ main ; sub(3); MOVLW 3 MOVLR 0 CALL sub ; pointers(); CALL pointers ;} SLEEP GOTO main _const1 MOVWF ci MOVLW 6 MOVWF TBLPTR+1 RRCF ci,W ANDLW 127 ADDLW 87 MOVWF TBLPTR BTFSC 0x04,Carry INCF TBLPTR+1,1 TABLRD 0,0,WREG TLRD 0,WREG BTFSC ci,0 TLRD 1,WREG RETURN DW 0x1 DW 0x2 DW 0x3E8 DW 0xFFDE DW 0x8AD0 DW 0x2 DW 0x64 DW 0x17 DW 0x0 DW 0x0 DW 0x7453 DW 0x6972 DW 0x676E DW 0x3120 DW 0x2300 DW 0x4841 DW 0x5050 DW 0x4152 DW 0x7420 DW 0x6261 DW 0x656C DW 0x6F20 DW 0x2066 DW 0x6F70 DW 0x6E69 DW 0x6574 DW 0x2072 DW 0x6F74 DW 0x7320 DW 0x7274 DW 0x6E69 DW 0x7367 DW 0x4D00 DW 0x6E6F DW 0x6164 DW 0x79 DW 0x7554 DW 0x7365 DW 0x6164 DW 0x79 DW 0x794D DW 0x7453 DW 0x6972 DW 0x676E DW 0x4800 DW 0x6C65 DW 0x6F6C DW 0x7720 DW 0x726F DW 0x646C DW 0x0 ORG 0x0600 DATA 0x0FFF DATA 0x0200 DATA 0x0100 DATA 0x0100 DATA 0x0182 DATA 0x000A DATA 0x01C8 DATA 0x03E8 DATA 0x0A0A DATA 0x009D DATA 0x005B DATA 0x0010 DATA 0x01F4 DATA 0x6548 DATA 0x6C6C DATA 0x206F DATA 0x6F77 DATA 0x6C72 DATA 0x0064 DATA 0x6E41 DATA 0x746F DATA 0x6568 DATA 0x2072 DATA 0x7473 DATA 0x6972 DATA 0x676E DATA 0x0A0D DATA 0x656D DATA 0x6772 DATA 0x6465 DATA 0x060B DATA 0x060D DATA 0x0613 END ; *** KEY INFO *** ; 0x0001 P0 23 word(s) 0 % : sub ; 0x0649 P0 65 word(s) 0 % : _const1 ; 0x0621 P0 6 word(s) 0 % : write ; 0x0627 P0 28 word(s) 0 % : pointers ; 0x0643 P0 6 word(s) 0 % : main ; RAM usage: 6 bytes (6 local), 896 bytes free ; Maximum call level: 2 ; Codepage 0 has 162 word(s) : 1 % ; Codepage 1 has 0 word(s) : 0 % ; Total of 162 code words (0 %)