X-Git-Url: http://gitweb.hugovil.com/?a=blobdiff_plain;f=src%2Fcommon%2Fsfr.c;h=7adc2cd339899a5e12ed648383731c31ec484976;hb=121bcb38f29409f10de63a68d86620c4beb75c97;hp=165ba2e1a86f916292aa04867f7c8f50f0c6a7fe;hpb=702da8f53198200be75f7b78011c3ffb1f4936a8;p=emu8051.git diff --git a/src/common/sfr.c b/src/common/sfr.c index 165ba2e..7adc2cd 100644 --- a/src/common/sfr.c +++ b/src/common/sfr.c @@ -3,19 +3,7 @@ * * Copyright (C) 2013 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 */ #if HAVE_CONFIG_H @@ -49,27 +37,28 @@ regwin_write_pc(int param, int val) { (void) param; /* Remove compiler warning about unused variable. */ - cpu8051.pc = (u_int16_t) val; + cpu8051.pc = (uint16_t) val; } static unsigned int regwin_read_timer(int timer_low_addr) { - return (memory_sfr_read8(timer_low_addr + 2) << 8) | - memory_sfr_read8(timer_low_addr); + return (mem_sfr_read8(timer_low_addr + 2) << 8) | + mem_sfr_read8(timer_low_addr); } static void regwin_write_timer(int timer_low_addr, int val) { - memory_sfr_write8(timer_low_addr + 2, (u_int8_t) ((val & 0x0000FFFF) >> 8)); - memory_sfr_write8(timer_low_addr, (u_int8_t) val); + mem_sfr_write8(timer_low_addr + 2, + (uint8_t) ((val & 0x0000FFFF) >> 8)); + mem_sfr_write8(timer_low_addr, (uint8_t) val); } -static u_int8_t +static uint8_t regwin_read_bank_offset(void) { - return memory_sfr_read8(_PSW_) & 0x18; + return mem_sfr_read8(_PSW_) & 0x18; } static unsigned int @@ -83,7 +72,7 @@ regwin_read_bank(int dummy) static void regwin_write_bank(int param, int bank_number) { - u_int8_t psw = memory_sfr_read8(_PSW_); + uint8_t psw = mem_sfr_read8(_PSW_); (void) param; /* Remove compiler warning about unused variable. */ @@ -92,21 +81,22 @@ regwin_write_bank(int param, int bank_number) bank_number = 0; } - memory_sfr_write8(_PSW_, (psw & ~0x18) | (bank_number << 3)); + mem_sfr_write8(_PSW_, (psw & ~0x18) | (bank_number << 3)); } /* Indirect read of R0 - R7 in current bank from internal memory. */ static unsigned int regwin_read_rx(int offset) { - return memory_read8(INT_MEM_ID, regwin_read_bank_offset() + offset); + return mem_read8(INT_MEM_ID, regwin_read_bank_offset() + offset); } /* Indirect write to R0 - R7 in current bank to internal memory. */ static void regwin_write_rx(int offset, int val) { - memory_write8(INT_MEM_ID, regwin_read_bank_offset() + offset, (u_int8_t) val); + mem_write8(INT_MEM_ID, regwin_read_bank_offset() + offset, + (uint8_t) val); } /* This array defines how to read value for each register. */ @@ -274,27 +264,28 @@ static struct regwin_infos_t regwin_infos[SFR_REGS] = { static unsigned int regwin_read_generic(int addr, int width) { - if (width == 2) - return memory_sfr_read8(addr); - else if (width == 4) { + if (width == 2) { + return mem_sfr_read8(addr); + } else if (width == 4) { /* Address is low address. */ - return (memory_sfr_read8(addr + 1) << 8) | - memory_sfr_read8(addr); - } else + return (mem_sfr_read8(addr + 1) << 8) | + mem_sfr_read8(addr); + } else { return 0xFFFFFFFF; + } } static void regwin_write_generic(int addr, int val, int width) { - if (width == 2) - memory_sfr_write8(addr, (u_int8_t) val); - else if (width == 4) { + if (width == 2) { + mem_sfr_write8(addr, (uint8_t) val); + } else if (width == 4) { /* Address is low address. */ - memory_sfr_write8(addr + 1, (u_int8_t) ((val & 0x0000FFFF) >> 8)); - memory_sfr_write8(addr, (u_int8_t) val); + mem_sfr_write8(addr + 1, (uint8_t) ((val & 0x0000FFFF) >> 8)); + mem_sfr_write8(addr, (uint8_t) val); } -}; +} int regwin_read(int row) @@ -317,9 +308,20 @@ regwin_read(int row) return val; } -void +int regwin_write(struct regwin_infos_t *p, int new) { + int max_value; + + max_value = (1 << (4 * p->w)) - 1; /* 16^w - 1 */ + + /* Check that the new value is not too large for the register type. */ + if (new > max_value) { + /* Display message for CLI version */ + printf("Value out of range\n"); + return -1; + } + if (p->write_func == NULL) { /* * Write register value using generic 8 or 16 bits write @@ -330,6 +332,8 @@ regwin_write(struct regwin_infos_t *p, int new) /* Write register value using custom function pointer. */ p->write_func(p->param, new); } + + return 0; } struct regwin_infos_t * @@ -342,7 +346,7 @@ sfr_get_infos(const char *regname) return ®win_infos[row]; } - return NULL; /* Programming error. */ + return NULL; /* Register not found. */ } struct regwin_infos_t *