#include <hidef.h> /* for EnableInterrupts macro */#include "derivative.h" /* include peripheral declarations */#define RX_BUF_SIZE 16#define RX_TOP_THRESH 14#define RX_BOT_THRESH 2void init(void);void rxISR(void);unsigned char rxBuffer[RX_BUF_SIZE]; // receive bufferunsigned char rx_read_index, rx_write_index; // read & write indexesunsigned char rx_buff_rem = RX_BUF_SIZE; // free buffer remainingunsigned char xon_xoff; // xon/xoff statusvoid main(void) { init(); EnableInterrupts; while (1) { __RESET_WATCHDOG(); asm wait; }}void init() { SCC1 = 0x40; // enable SCI SCC2 = 0x2C; // enable rx, tx, rx int SCBR = 0x03; // set BAUD}#pragma TRAP_PROCvoid rxISR() { SCS1; // dummy read SCRF flag to clear rxBuffer[rx_write_index++] = SCDR; // write incoming byte to buffer rx_buff_rem--; // decrement buffer remaining if (rx_write_index == RX_BUF_SIZE) // if end of buffer is reached rx_write_index = 0; // then start at beginning if (rx_buff_rem == RX_BOT_THRESH) { // if buffer size reaches xoff threshold while((SCS1 & 0x80) == 0x00); // wait for Tx ready SCDR = 0x13; // send XOFF signal }}
0000 8b [2] PSHH 34: SCS1; // dummy read SCRF flag to clear 0001 b600 [3] LDA _SCS1 35: rxBuffer[rx_write_index++] = SCDR; // write incoming byte to buffer 0003 be00 [3] LDX rx_write_index 0005 b600 [3] LDA _SCDR 0007 8c [1] CLRH 0008 e700 [3] STA @rxBuffer,X 000a 5c [1] INCX 000b bf00 [3] STX rx_write_index 36: rx_buff_rem--; // decrement buffer remaining 000d 3a00 [4] DEC rx_buff_rem 37: if (rx_write_index == RX_BUF_SIZE) // if end of buffer is reached 000f a310 [2] CPX #16 0011 2602 [3] BNE L15 ;abs = 0015 38: rx_write_index = 0; // then start at beginning 0013 3f00 [3] CLR rx_write_index 0015 [5] L15: 39: if (rx_buff_rem == RX_BOT_THRESH) { // if buffer size reaches xoff threshold 0015 b600 [3] LDA rx_buff_rem 0017 a102 [2] CMP #2 0019 2606 [3] BNE L21 ;abs = 0021 001b [5] L1B: 40: while((SCS1 & 0x80) == 0x00); // wait for Tx ready 001b 0f00fd [5] BRCLR 7,_SCS1,L1B ;abs = 001b 41: SCDR = 0x13; // send XOFF signal 001e 6e1300 [4] MOV #19,_SCDR 0021 [5] L21: 42: } 43: } 0021 8a [2] PULH 0022 80 [7] RTI
I wouldn't call myself an HC08 assembly expert, but this looks pretty good to me. I count 35 bytes, and in the most common case there will be 57 CPU cycles (counting the 16 interrupt overhead cycles - so really 41 cycles).
I'm now expecting a severe lashing from the Freescale community.
Message Edited by imajeff on 2006-06-08 11:53 AM
Hello Jefferson,
The register names are correct for use with HC08 assembler. So they are different than the HC9S12 register names, but so are the assembly instructions. We just need to learn the differences.
Regards,
Mac
Message Edited by bigmac on 2006-06-09 04:08 AM
Yes. This was CodeWarrior and the register names are consistent with the HC08 KX8 datasheet. Rocco asked for it to be HC08 assembly so he could compare it with something he already had written. Sorry about posting HC08 code in the 16 bit forum, but the topic itself could have gone in either place.
I didn't finish writing a full implementation of XON/XOFF either. To be a fair comparison I should really do the whole thing. From what is there, however, I think I would have done something very similar to what the compiler did - had I done it in assembly.
00004038 : 4038: 18 0b 40 00 movb #64, ca __bss_size+0xb1> 403c: ca 403d: 18 0b 2c 00 movb #44, cb __bss_size+0xb2> 4041: cb 4042: 18 03 00 34 movw #34 __bss_size+0x1b>, c8 __bss_size+0xaf> 4046: 00 c8 4048: 3d rts 00004049 : 4049: 18 01 ae 10 movw 1002 _.tmp>, 2,-SP 404d: 02 404e: 18 01 ae 10 movw 1004 _.z>, 2,-SP 4052: 04 4053: 18 01 ae 10 movw 1006 _.xy>, 2,-SP 4057: 06 4058: f6 00 cc ldab cc __bss_size+0xb3> 405b: cd 10 09 ldy #1009 405e: f6 10 1a ldab 101a 4061: 19 ed aby 4063: 18 09 40 00 movb cf __bss_size+0xb6>, 0,Y 4067: cf 4068: 72 10 1a inc 101a 406b: 73 10 00 dec 1000 __data_section_start> 406e: f6 10 1a ldab 101a 4071: c1 10 cmpb #16 4073: 26 03 bne 4078 4075: 79 10 1a clr 101a 4078: f6 10 00 ldab 1000 __data_section_start> 407b: c1 02 cmpb #2 407d: 26 0a bne 4089 407f: f6 00 cc ldab cc __bss_size+0xb3> 4082: 2a fb bpl 407f 4084: 18 0b 13 00 movb #19, cf __bss_size+0xb6> 4088: cf 4089: 18 05 b1 10 movw 2,SP+, 1006 _.xy> 408d: 06 408e: 18 05 b1 10 movw 2,SP+, 1004 _.z> 4092: 04 4093: 18 05 b1 10 movw 2,SP+, 1002 _.tmp> 4097: 02 4098: 0b rti
Yes, the C has suddenly become poorly written :-)
Seriously, what did you have to change to get it to compile? There are 3 pushes to the stack that happen at the beginning of the ISR and 3 pulls at the end that seem to serve no purpose. The rest of it is bloated and messy for sure. I'm not impressed.
The complaint about the compilers requiring slightly different syntax is a fair one - but I thought that all of the ANSI C compilers should use exactly the same syntax. I'm not sure if the code I posted is ANSI C or not, but I thought it was. Where did you get the GCC compiler? I was not aware of it.
rhinoceroshead wrote:Yes, the C has suddenly become poorly written :smileyhappy:
Seriously, what did you have to change to get it to compile? There are 3 pushes to the stack that happen at the beginning of the ISR and 3 pulls at the end that seem to serve no purpose. The rest of it is bloated and messy for sure. I'm not impressed.
The complaint about the compilers requiring slightly different syntax is a fair one - but I thought that all of the ANSI C compilers should use exactly the same syntax. I'm not sure if the code I posted is ANSI C or not, but I thought it was. Where did you get the GCC compiler? I was not aware of it.
Message Edited by imajeff on 2006-06-0908:37 AM
Message Edited by imajeff on 2006-06-09 08:42 AM
Message Edited by imajeff on 2006-06-09 08:44 AM
rhinoceroshead wrote:. . . He said C compilers used to be written very poorly but nowadays even the best assembly programmers can rarely beat them because the compilers have gotten so good.
He is somewhat correct, depending on the architecture.
rhinoceroshead wrote:. . . He said C compilers used to be written very poorly but nowadays even the best assembly programmers can rarely beat them because the compilers have gotten so good.
Message Edited by imajeff on 06-01-2006 03:28 PM
Hi,
I always use assembly too.
I am always amused by the many posts to this forum that start with lines like:
I am having trouble implementing this in C or trouble with processor expert, beans, initialisation blah blah blah to do such and such.
At this point I usually mumble to myself:
Well I could show you how to do it in a few lines of assembler, but I would not want to slow down/corrupt your rapid, abstract development methodology by dirtying it with horrible low level assembly language.
Regards David
Thank you,
I would also say I'm old school, and by way of hardward designer. I have been using the 68HC912 for a few years now, and had always written in assembly. I just need to feel comforatable about instruction versus action. I got the DEMO9S12NE64 with the notion that I would be up and running in a couple of days. I must confess, I still haven't been able to load code to toggle an I/O, oh yea, using ColdWorrior.
I would like to open the browser and have the target communicate through http. Seems to me that this would ease the programming effort on the PC. I'll go through the TCP/IP stact learning curve, and stick with assembly.
George