Files
V2TMUMemTester/Utils.h

157 lines
3.3 KiB
C

/* V2MemTest - A CLI Tool to test & fix Voodoo² TMU System
* Copyright (C) 2026 ChaCha
*
* 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, see <https://www.gnu.org/licenses/>.
*/
#ifndef _DEF_UTILS_H_
#define _DEF_UTILS_H_
#include <stdint.h>
#include "V2MemTest.h"
#define ErrorCheck() \
do{ \
if(Status < 0) \
{ \
if(Status <= E_INVALID_ERROR_CODE) \
fprintf(stderr,"Error detected at %s:%d\t%s", \
__FILE__,__LINE__,szErrorMsg[DEF_UNKNOWN_ERROR_IDX]); \
else \
fprintf(stderr,"Error detected at %s:%d\t%s", \
__FILE__,__LINE__, \
szErrorMsg[DEF_GET_ERROR_IDX(Status)]); \
return Status; \
} \
}while(0)
#define ErrorCheck_gotoCleanUp() \
do{ \
if(Status < 0) \
{ \
if(Status <= E_INVALID_ERROR_CODE) \
fprintf(stderr,"Error detected at %s:%d\t%s", \
__FILE__,__LINE__,szErrorMsg[DEF_UNKNOWN_ERROR_IDX]); \
else \
fprintf(stderr,"Error detected at %s:%d\t%s", \
__FILE__,__LINE__, \
szErrorMsg[DEF_GET_ERROR_IDX(Status)]); \
goto CleanUp; \
} \
}while(0)
static inline unsigned char
count_bit32(const uint32_t ulVal)
{
/*
unsigned char cpt = 0;
for(uint32_t idxBit = 0x80000000u; idxBit != 0; idxBit >>= 1)
if(ulVal & idxBit)
cpt++;
return cpt;
*/
return __builtin_popcount(ulVal);
}
static inline unsigned char
count_bit8(const uint8_t ulVal)
{
/*
unsigned char cpt = 0;
for(uint8_t idxBit = 0x80u; idxBit != 0; idxBit >>= 1)
if(ulVal & idxBit)
cpt++;
return cpt;
*/
return __builtin_popcount(ulVal);
}
void
sprintbin32( char * const szBuff,
const uint32_t ulVal,
const unsigned char bGroupByBytes);
void
printbin32( const uint32_t ulVal,
const unsigned char bGroupByBytes);
void
sprintbin32Info(char * const szBuff,
const uint32_t ulVal,
const unsigned short uhb,
const unsigned short ulb);
void
printbin32Info( const uint32_t ulVal,
const unsigned short uhb,
const unsigned short ulb);
uint32_t
get_notnull_random();
uint32_t
get_notnull_random_balanced();
uint8_t
get_notnull8_random_balanced();
uint32_t
get_notnull_random_balanced_mByte();
static inline void
mmio_fastread32(uint32_t * dst,
volatile const uint32_t * src,
size_t words)
{
while(words>=4) {
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = src[3];
words-=4;
src+=4;
dst+=4;
}
while(words--) {
*dst++ = *src++;
}
}
static inline void
mmio_fastwrite32( volatile uint32_t * dst,
const uint32_t * src,
size_t words)
{
while(words>=4) {
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = src[3];
words-=4;
src+=4;
dst+=4;
}
while(words--) {
*dst++ = *src++;
}
}
static inline double dComputeDistance32(const uint32_t u32A,const uint32_t u32B)
{
return (double)__builtin_popcount( u32A ^ u32B);
}
#endif //_DEF_UTILS_H_