X-Git-Url: http://gitweb.hugovil.com/?a=blobdiff_plain;f=src%2Fcpu8051.c;h=1fd02514f5c0455431554dcae406e5696dec67b6;hb=ce24c216aa7eaf099dda2c9efeb9603a4076c93e;hp=30c8cca7c1a9863afe8c28f6984a8462c9d47106;hpb=dd92f3969f3537fc1dd13926e7ae1014d27513ab;p=emu8051.git diff --git a/src/cpu8051.c b/src/cpu8051.c index 30c8cca..1fd0251 100644 --- a/src/cpu8051.c +++ b/src/cpu8051.c @@ -283,18 +283,27 @@ process_timer(uint8_t tl, uint8_t th, uint8_t tf_mask, uint8_t TR, uint8_t mode, uint8_t GATE, uint32_t TimerCounter) { unsigned int tmp; + unsigned int prescaler; switch (mode) { case 0: - /* Mode 0, 13-bits counter. */ - tmp = cpu8051_ReadD(th) * 0x100 + cpu8051_ReadD(tl); - tmp++; - tmp &= 0x1FFF; /* We keep only 13 bits */ - - if (tmp == 0) /* If overflow set TF0 */ - cpu8051_WriteD(_TCON_, cpu8051_ReadD(_TCON_) | tf_mask); - cpu8051_WriteD(_TH0_, tmp / 0x100); - cpu8051_WriteD(_TL0_, tmp & 0xFF); + /* Mode 0, 8-bit timer "TH" with "TL" as 5-bit prescaler. */ + prescaler = cpu8051_ReadD(tl); + prescaler++; + prescaler &= 0x1F; /* We keep only 5 bits */ + cpu8051_WriteD(tl, prescaler); + + if (prescaler == 0) { + /* If overflow, increment TH */ + tmp = cpu8051_ReadD(th); + tmp++; + tmp &= 0xFF; /* We keep only 8 bits */ + + if (tmp == 0) /* If overflow set TF */ + cpu8051_WriteD(_TCON_, cpu8051_ReadD(_TCON_) | tf_mask); + + cpu8051_WriteD(th, tmp); + } break; case 1: /* Mode 1, 16-bits counter */ @@ -303,8 +312,8 @@ process_timer(uint8_t tl, uint8_t th, uint8_t tf_mask, uint8_t TR, uint8_t mode, tmp &= 0xFFFF; /* We keep only 16 bits */ if (tmp == 0) /* If overflow set TF0 */ cpu8051_WriteD(_TCON_, cpu8051_ReadD(_TCON_) | tf_mask); - cpu8051_WriteD(_TH0_, (tmp / 0x100)); - cpu8051_WriteD(_TL0_, (tmp & 0xFF)); + cpu8051_WriteD(th, (tmp / 0x100)); + cpu8051_WriteD(tl, (tmp & 0xFF)); break; case 2: /* Mode 2, 8-bits counter with Auto-Reload */ @@ -492,7 +501,7 @@ cpu8051_get_instruction_size(unsigned char opcode) return InstSizesTbl[opcode]; } -/* Display instruction menmonic. */ +/* Display instruction mnemonic. */ void cpu8051_disasm_mnemonic(unsigned char OpCode, char *buf) { @@ -630,7 +639,7 @@ cpu8051_Disasm(unsigned int Address, char *Text) for (; len < 17;) len += sprintf(&Text[len], " "); - /* Display instruction menmonic. */ + /* Display instruction mnemonic. */ len += sprintf(&Text[len], "%s ", InstTextTbl[InstTypesTbl[OpCode]]); ArgTblOfs = OpCode << 2;