X-Git-Url: http://gitweb.hugovil.com/?a=blobdiff_plain;f=src%2Fcommon%2Ftimers.c;h=89eb42db1d8e912b18538837eed46068d677dec3;hb=9cd867c94aabb48f44658a7a4d6832115c876b74;hp=68a92bc7f7b02a407db4bab94d3fc629183faf1a;hpb=1eb382f72510d50b3636fb88c4bfaf17183672b6;p=emu8051.git diff --git a/src/common/timers.c b/src/common/timers.c index 68a92bc..89eb42d 100644 --- a/src/common/timers.c +++ b/src/common/timers.c @@ -4,19 +4,7 @@ * Copyright (C) 1999 Jonathan St-André * Copyright (C) 1999 Hugo Villeneuve * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. + * This file is released under the GPLv2 */ #include @@ -31,35 +19,64 @@ #include "psw.h" #include "options.h" #include "instructions_8051.h" +#include "timers.h" + +static int gp_timer_value[GP_TIMERS_COUNT]; extern struct options_t options; +void +gp_timer_reset(int id) +{ + log_debug("gp timer %d reset", id); + gp_timer_value[id] = 0; +} + +void +gp_timers_increment(int count) +{ + int id; + + log_debug("gp timers increment"); + + for (id = 0; id < GP_TIMERS_COUNT; id++) + gp_timer_value[id] += count; +} + +int +gp_timer_read(int id) +{ + return gp_timer_value[id]; +} + static void timer_increment_check_overflow(uint8_t counter_address, uint8_t tf_mask) { unsigned int tmp; - tmp = cpu8051_ReadD(counter_address); + tmp = mem_read_direct(counter_address); tmp++; tmp &= 0xFF; if (tmp == 0) { /* If overflow set TFx */ - cpu8051_WriteD(_TCON_, cpu8051_ReadD(_TCON_) | tf_mask); + mem_write_direct(_TCON_, + mem_read_direct(_TCON_) | tf_mask); } - cpu8051_WriteD(counter_address, tmp); /* Save new value. */ + mem_write_direct(counter_address, tmp); /* Save new value. */ } static void -timer_with_prescaler(uint8_t tl, uint8_t th, uint8_t tf_mask, int prescaler_width) +timer_with_prescaler(uint8_t tl, uint8_t th, uint8_t tf_mask, + int prescaler_width) { unsigned int prescaler; - prescaler = cpu8051_ReadD(tl); + prescaler = mem_read_direct(tl); prescaler++; prescaler &= (1 << prescaler_width) - 1; /* Keep only required bits */ - cpu8051_WriteD(tl, prescaler); + mem_write_direct(tl, prescaler); if (prescaler == 0) timer_increment_check_overflow(th, tf_mask); @@ -67,7 +84,7 @@ timer_with_prescaler(uint8_t tl, uint8_t th, uint8_t tf_mask, int prescaler_widt static void process_timer(uint8_t tl, uint8_t th, uint8_t tf_mask, uint8_t TR, uint8_t mode, - uint8_t GATE, uint32_t TimerCounter) + uint8_t gate, uint32_t timer_counter) { unsigned int tmp; @@ -82,15 +99,18 @@ process_timer(uint8_t tl, uint8_t th, uint8_t tf_mask, uint8_t TR, uint8_t mode, break; case 2: /* Mode 2, 8-bits counter with Auto-Reload */ - tmp = cpu8051_ReadD(tl); + tmp = mem_read_direct(tl); tmp++; tmp &= 0xFF; if (tmp == 0) { /* If overflow -> reload and set TF0 */ - cpu8051_WriteD(_TCON_, cpu8051_ReadD(_TCON_) | tf_mask); - cpu8051_WriteD(tl, cpu8051_ReadD(th)); - } else - cpu8051_WriteD(tl, tmp); + mem_write_direct( + _TCON_, + mem_read_direct(_TCON_) | tf_mask); + mem_write_direct(tl, mem_read_direct(th)); + } else { + mem_write_direct(tl, tmp); + } break; case 3: /* @@ -102,11 +122,11 @@ process_timer(uint8_t tl, uint8_t th, uint8_t tf_mask, uint8_t TR, uint8_t mode, if (tl == _TL1_) break; - if (TR && !GATE && !TimerCounter) + if (TR && !gate && !timer_counter) timer_increment_check_overflow(tl, tf_mask); /* TH0 uses TR1 et TF1. */ - TR = cpu8051_ReadD(_TCON_) & 0x40; + TR = mem_read_direct(_TCON_) & 0x40; if (TR) timer_increment_check_overflow(th, 0x80); @@ -115,30 +135,32 @@ process_timer(uint8_t tl, uint8_t th, uint8_t tf_mask, uint8_t TR, uint8_t mode, } } -/* Run timers */ +/* Run 8051 timers */ void timers_check(void) { - unsigned int TR; - unsigned int MODE; - unsigned int GATE; - unsigned int TimerCounter; + unsigned int tr; + unsigned int mode; + unsigned int gate; + unsigned int timer_counter; /* Timer 0 */ - TR = cpu8051_ReadD(_TCON_) & 0x10; - MODE = cpu8051_ReadD(_TMOD_) & 0x03; - GATE = cpu8051_ReadD(_TMOD_) & 0x08; - TimerCounter = cpu8051_ReadD(_TMOD_) & 0x04; + tr = mem_read_direct(_TCON_) & 0x10; + mode = mem_read_direct(_TMOD_) & 0x03; + gate = mem_read_direct(_TMOD_) & 0x08; + timer_counter = mem_read_direct(_TMOD_) & 0x04; - if ((TR && !GATE && !TimerCounter) || (MODE == 3)) - process_timer(_TL0_, _TH0_, 0x20, TR, MODE, GATE, TimerCounter); + if ((tr && !gate && !timer_counter) || (mode == 3)) + process_timer(_TL0_, _TH0_, 0x20, tr, mode, gate, + timer_counter); /* Timer 1 */ - TR = cpu8051_ReadD(_TCON_) & 0x40; - MODE = (cpu8051_ReadD(_TMOD_) & 0x30) >> 4 ; - GATE = cpu8051_ReadD(_TMOD_) & 0x80; - TimerCounter = cpu8051_ReadD(_TMOD_) & 0x40; - - if (TR && !GATE && !TimerCounter) - process_timer(_TL1_, _TH1_, 0x80, TR, MODE, GATE, TimerCounter); + tr = mem_read_direct(_TCON_) & 0x40; + mode = (mem_read_direct(_TMOD_) & 0x30) >> 4; + gate = mem_read_direct(_TMOD_) & 0x80; + timer_counter = mem_read_direct(_TMOD_) & 0x40; + + if (tr && !gate && !timer_counter) + process_timer(_TL1_, _TH1_, 0x80, tr, mode, gate, + timer_counter); }