opcodes2c.pl: Add command line options
[emu8051.git] / src / common / sfr.c
index 1bbeb8e..7adc2cd 100644 (file)
@@ -3,19 +3,7 @@
  *
  * Copyright (C) 2013 Hugo Villeneuve <hugo@hugovil.com>
  *
- * 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
 static unsigned int
 regwin_read_pc(int dummy)
 {
+       (void) dummy; /* Remove compiler warning about unused variable. */
+
        return cpu8051.pc;
 }
 
 static void
 regwin_write_pc(int param, int val)
 {
-       cpu8051.pc = (u_int16_t) val;
+       (void) param; /* Remove compiler warning about unused variable. */
+
+       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
 regwin_read_bank(int dummy)
 {
+       (void) dummy; /* Remove compiler warning about unused variable. */
+
        return regwin_read_bank_offset() >> 3;
 }
 
 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. */
 
        if ((bank_number < 0) || (bank_number > 3)) {
                log_info("Error: invalid bank number: %d", 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. */
@@ -266,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)
@@ -309,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
@@ -322,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 *
@@ -334,7 +346,7 @@ sfr_get_infos(const char *regname)
                        return &regwin_infos[row];
        }
 
-       return NULL; /* Programming error. */
+       return NULL; /* Register not found. */
 }
 
 struct regwin_infos_t *