/* Copyright (C) 2008 - Ruben Niederhagen
 * 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 3
 * 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, you will find it online at
 * http://www.gnu.org/licenses/gpl.html
 */

#include <spu_mfcio.h>
#include <stdio.h>

#include "include/spu/dma_debug.h"
#include "src/common/common.h"
#include "src/spu/types.h"

extern volatile spu_data_t spu_data __attribute__((aligned (16)));

void mfc_get_debug(volatile void *ls, uint64_t ea, uint32_t size,
						  uint32_t tag, uint32_t tid, uint32_t rid, char *file, int line)
{
	if (((uint32_t)ls & 0xF) != (ea & 0xF))
		printf("-%i-%s:%i: Effective address does not match local store address! ea: 0x%llx; ls: 0x%x\n",
				 spu_data.my_id, file, line, (uint64_t)ea, (uint32_t)ls);
	
	if ((size != 1) && (size != 2) && (size != 4) && (size != 8) && ((size & 0xF) > 0))
		printf("-%i-%s:%i: Size must be 1, 2, 4, 8 or i*16, but it is %u!\n",  spu_data.my_id, 
				 file, line, size);

	if (size > (16*1024))
		 printf("-%i-%s:%i: Size must be less than %u, but it is %u!\n",  spu_data.my_id,  
             file, line, 16*1024, size);

	mfc_get((char*)ls, ea, size, tag, tid, rid);
}

void mfc_put_debug(volatile void *ls, uint64_t ea, uint32_t size, 
						  uint32_t tag, uint32_t tid, uint32_t rid, char *file, int line)
{
	if (((uint32_t)ls & 0xF) != (ea & 0xF))
		printf("-%i-%s:%i: Effective address does not match local store address! ea: 0x%llx; ls: 0x%x\n",
				 spu_data.my_id, file, line, (uint64_t)ea, (uint32_t)ls);
	
	if ((size != 1) && (size != 2) && (size != 4) && (size != 8) && ((size & 0xF) > 0))
		printf("-%i-%s:%i: Size must be 1, 2, 4, 8 or i*16, but it is %u!\n",  spu_data.my_id, 
				 file, line, size);

	if (size > (16*1024))
       printf("-%i-%s:%i: Size must be less than %u, but it is %u!\n",  spu_data.my_id,               
             file, line, 16*1024, size);

	mfc_put((char*)ls, ea, size, tag, tid, rid);
}

