Fix checkpatch warnings
[emu8051.git] / src / common / sfr.c
index 165ba2e..0e609f9 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
@@ -62,7 +50,8 @@ regwin_read_timer(int 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 + 2,
+                         (u_int8_t) ((val & 0x0000FFFF) >> 8));
        memory_sfr_write8(timer_low_addr, (u_int8_t) val);
 }
 
@@ -106,7 +95,8 @@ regwin_read_rx(int offset)
 static void
 regwin_write_rx(int offset, int val)
 {
-       memory_write8(INT_MEM_ID, regwin_read_bank_offset() + offset, (u_int8_t) val);
+       memory_write8(INT_MEM_ID, regwin_read_bank_offset() + offset,
+                     (u_int8_t) val);
 }
 
 /* This array defines how to read value for each register. */
@@ -274,27 +264,29 @@ static struct regwin_infos_t regwin_infos[SFR_REGS] = {
 static unsigned int
 regwin_read_generic(int addr, int width)
 {
-       if (width == 2)
+       if (width == 2) {
                return memory_sfr_read8(addr);
-       else if (width == 4) {
+       else if (width == 4) {
                /* Address is low address. */
                return (memory_sfr_read8(addr + 1) << 8) |
                        memory_sfr_read8(addr);
-       } else
+       } else {
                return 0xFFFFFFFF;
+       }
 }
 
 static void
 regwin_write_generic(int addr, int val, int width)
 {
-       if (width == 2)
+       if (width == 2) {
                memory_sfr_write8(addr, (u_int8_t) val);
-       else if (width == 4) {
+       else if (width == 4) {
                /* Address is low address. */
-               memory_sfr_write8(addr + 1, (u_int8_t) ((val & 0x0000FFFF) >> 8));
+               memory_sfr_write8(addr + 1,
+                                 (u_int8_t) ((val & 0x0000FFFF) >> 8));
                memory_sfr_write8(addr, (u_int8_t) val);
        }
-};
+}
 
 int
 regwin_read(int row)
@@ -317,9 +309,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 +333,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 +347,7 @@ sfr_get_infos(const char *regname)
                        return &regwin_infos[row];
        }
 
-       return NULL; /* Programming error. */
+       return NULL; /* Register not found. */
 }
 
 struct regwin_infos_t *