2011-03-11 00:49:07 +00:00
|
|
|
/* MSPDebug - debugging tool for MSP430 MCUs
|
|
|
|
* Copyright (C) 2009, 2010 Daniel Beer
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "simio_device.h"
|
|
|
|
#include "simio_hwmult.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "expr.h"
|
|
|
|
|
2020-09-05 02:56:37 +00:00
|
|
|
/* Multiplier register offsets from base addr */
|
|
|
|
#define MPY 0x0 /* Multiply Unsigned/Operand 1 */
|
|
|
|
#define MPYS 0x2 /* Multiply Signed/Operand 1 */
|
|
|
|
#define MAC 0x4 /* Multiply Unsigned and Accumulate/Operand 1 */
|
|
|
|
#define MACS 0x6 /* Multiply Signed and Accumulate/Operand 1 */
|
|
|
|
#define OP2 0x8 /* Operand 2 */
|
|
|
|
#define RESLO 0xA /* Result Low Word */
|
|
|
|
#define RESHI 0xC /* Result High Word */
|
|
|
|
#define SUMEXT 0xE /* Sum Extend */
|
2011-03-11 00:49:07 +00:00
|
|
|
|
|
|
|
struct hwmult {
|
|
|
|
struct simio_device base;
|
|
|
|
|
|
|
|
int mode;
|
|
|
|
|
2020-09-05 02:56:37 +00:00
|
|
|
address_t base_addr;
|
|
|
|
|
2011-03-11 00:49:07 +00:00
|
|
|
uint16_t op1;
|
|
|
|
uint16_t op2;
|
|
|
|
uint32_t result;
|
|
|
|
uint16_t sumext;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct simio_device *hwmult_create(char **arg_text)
|
|
|
|
{
|
|
|
|
struct hwmult *h = malloc(sizeof(*h));
|
|
|
|
|
2011-11-14 18:38:19 +00:00
|
|
|
(void)arg_text;
|
|
|
|
|
2011-03-11 00:49:07 +00:00
|
|
|
if (!h) {
|
|
|
|
pr_error("hwmult: can't allocate memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(h, 0, sizeof(*h));
|
|
|
|
h->base.type = &simio_hwmult;
|
|
|
|
|
2020-09-05 02:56:37 +00:00
|
|
|
h->base_addr = 0x130;
|
|
|
|
|
2011-03-11 00:49:07 +00:00
|
|
|
return (struct simio_device *)h;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hwmult_destroy(struct simio_device *dev)
|
|
|
|
{
|
|
|
|
free(dev);
|
|
|
|
}
|
|
|
|
|
2020-09-05 02:56:37 +00:00
|
|
|
static int config_addr(address_t *addr, char **arg_text)
|
|
|
|
{
|
|
|
|
char *text = get_arg(arg_text);
|
|
|
|
|
|
|
|
if (!text) {
|
|
|
|
printc_err("hwmult: config: expected address\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expr_eval(text, addr) < 0) {
|
|
|
|
printc_err("hwmult: can't parse address: %s\n", text);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hwmult_config(struct simio_device *dev,
|
|
|
|
const char *param, char **arg_text)
|
|
|
|
{
|
|
|
|
struct hwmult *h = (struct hwmult *)dev;
|
|
|
|
|
|
|
|
if (!strcasecmp(param, "base"))
|
|
|
|
return config_addr(&h->base_addr, arg_text);
|
|
|
|
|
|
|
|
printc_err("hwmult: config: unknown parameter: %s\n", param);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hwmult_info(struct simio_device *dev)
|
|
|
|
{
|
|
|
|
struct hwmult *h = (struct hwmult *)dev;
|
|
|
|
|
|
|
|
printc("Base address: 0x%04x\n\n", h->base_addr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-11 00:49:07 +00:00
|
|
|
static void do_multiply(struct hwmult *h)
|
|
|
|
{
|
|
|
|
uint32_t im;
|
2013-05-25 12:13:03 +00:00
|
|
|
uint64_t temp = 0;
|
2011-03-11 00:49:07 +00:00
|
|
|
|
|
|
|
/* Multiply */
|
|
|
|
if (h->mode & 2)
|
|
|
|
im = (int16_t)h->op1 * (int16_t)h->op2;
|
|
|
|
else
|
|
|
|
im = h->op1 * h->op2;
|
|
|
|
|
|
|
|
/* Accumulate or store */
|
|
|
|
if (h->mode & 4)
|
2013-05-25 12:13:03 +00:00
|
|
|
{
|
|
|
|
temp = (uint64_t)h->result + im;
|
|
|
|
h->result = temp;
|
|
|
|
}
|
2011-03-11 00:49:07 +00:00
|
|
|
else
|
|
|
|
h->result = im;
|
|
|
|
|
|
|
|
/* Set SUMEXT */
|
2013-05-25 12:13:03 +00:00
|
|
|
if (h->mode & 2) /* MPYS and MACS */
|
2011-03-11 00:49:07 +00:00
|
|
|
h->sumext = (h->result & 0x80000000) ? 0xffff : 0;
|
2013-05-25 12:13:03 +00:00
|
|
|
else if(h->mode == MAC)
|
|
|
|
h->sumext = temp >> 32;
|
|
|
|
else /* MPY */
|
2011-03-11 00:49:07 +00:00
|
|
|
h->sumext = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hwmult_write(struct simio_device *dev, address_t addr, uint16_t data)
|
|
|
|
{
|
|
|
|
struct hwmult *h = (struct hwmult *)dev;
|
|
|
|
|
2020-09-05 02:56:37 +00:00
|
|
|
if (addr < h->base_addr) return 1;
|
|
|
|
|
|
|
|
addr -= h->base_addr;
|
|
|
|
|
2011-03-11 00:49:07 +00:00
|
|
|
switch (addr) {
|
|
|
|
case RESHI:
|
|
|
|
h->result = (h->result & 0xffff) | ((uint32_t)data << 16);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case RESLO:
|
|
|
|
h->result = (h->result & 0xffff0000) | data;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case OP2:
|
|
|
|
h->op2 = data;
|
|
|
|
do_multiply(h);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case MPY:
|
|
|
|
case MPYS:
|
|
|
|
case MAC:
|
|
|
|
case MACS:
|
|
|
|
h->op1 = data;
|
|
|
|
h->mode = addr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hwmult_read(struct simio_device *dev, address_t addr, uint16_t *data)
|
|
|
|
{
|
|
|
|
struct hwmult *h = (struct hwmult *)dev;
|
|
|
|
|
2020-09-05 02:56:37 +00:00
|
|
|
if (addr < h->base_addr) return 1;
|
|
|
|
|
|
|
|
addr -= h->base_addr;
|
|
|
|
|
2011-03-11 00:49:07 +00:00
|
|
|
switch (addr) {
|
|
|
|
case MPY:
|
|
|
|
case MPYS:
|
|
|
|
case MAC:
|
|
|
|
case MACS:
|
|
|
|
*data = h->op1;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case OP2:
|
|
|
|
*data = h->op2;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case RESLO:
|
|
|
|
*data = h->result & 0xffff;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case RESHI:
|
|
|
|
*data = h->result >> 16;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SUMEXT:
|
|
|
|
*data = h->sumext;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct simio_class simio_hwmult = {
|
|
|
|
.name = "hwmult",
|
|
|
|
.help =
|
2020-09-05 02:56:37 +00:00
|
|
|
"This module simulates the hardware multiplier.\n"
|
|
|
|
"Config arguments are:\n"
|
|
|
|
" base <address>\n"
|
|
|
|
" Set the peripheral base address.\n",
|
2011-03-11 00:49:07 +00:00
|
|
|
.create = hwmult_create,
|
|
|
|
.destroy = hwmult_destroy,
|
2020-09-05 02:56:37 +00:00
|
|
|
.config = hwmult_config,
|
|
|
|
.info = hwmult_info,
|
2011-03-11 00:49:07 +00:00
|
|
|
.write = hwmult_write,
|
|
|
|
.read = hwmult_read
|
|
|
|
};
|