/*
 ***************************************************************************************************** 
 * M32C Watchdog simulation.
 * 
 * State: Minimal implementation allowing the system to reboot.
 *
 * Copyright 2010 Jochen Karrer. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 *   1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 * 
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY Jochen Karrer ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of Jochen Karrer.
 *
 ***************************************************************************************************** 
 */
#include <bus.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include "sgstring.h"
#include "clock.h"
#include "cycletimer.h"
#include "signode.h"
#include "watchdog_m32c.h"

#define REG_WDTS	(0x0e)
typedef struct Watchdog {
	BusDevice bdev;
} Watchdog;


static uint32_t
wdts_read(void *clientData,uint32_t address,int rqlen)
{
        return 0;
}

#include "cpu_m32c.h"
static void
wdts_write(void *clientData,uint32_t value,uint32_t address,int rqlen)
{
	if(value == 0) {
		fprintf(stderr,"Watchdog reset\n");
		fprintf(stderr,"PC 0x%06x\n",M32C_REG_PC);
		exit(0);
	}
}

static void
M32CWatchdog_Unmap(void *owner,uint32_t base,uint32_t mask)
{
        IOH_Delete16(REG_WDTS);
}

static void
M32CWatchdog_Map(void *owner,uint32_t base,uint32_t mask,uint32_t mapflags)
{
	Watchdog *wdg = owner;
	uint32_t flags = IOH_FLG_HOST_ENDIAN | IOH_FLG_PRD_RARP | IOH_FLG_PA_CBSE;
        IOH_New16f(REG_WDTS,wdts_read,wdts_write,wdg,flags);
}

BusDevice *
M32C_WatchdogNew(const char *name)
{
	Watchdog *wdg = sg_new(Watchdog);
        wdg->bdev.first_mapping=NULL;
        wdg->bdev.Map=M32CWatchdog_Map;
        wdg->bdev.UnMap=M32CWatchdog_Unmap;
        wdg->bdev.owner=wdg;
        wdg->bdev.hw_flags=MEM_FLAG_WRITABLE|MEM_FLAG_READABLE;
	return &wdg->bdev;
}
