Files
V2TMUMemTester/Utils.c

134 lines
2.9 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/>.
*/
#define _BSD_SOURCE 1
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "Utils.h"
void
sprintbin32( char * szBuff,
const uint32_t ulVal,
const unsigned char bGroupByBytes)
{
for(uint32_t idxBit = 1u<<31 ; idxBit > 0 ; idxBit >>= 1)
{
if(bGroupByBytes)
switch(idxBit)
{
case 1u << 23:
case 1u << 15:
case 1u << 7:
*(szBuff++) = ' ';
}
*(szBuff++) = ulVal & idxBit ? '1' : '0';
}
*szBuff = '\0';
}
void
printbin32( const uint32_t ulVal,
const unsigned char bGroupByBytes)
{
char szBuff[32 + 3 + 1 + 1];
sprintbin32(szBuff,ulVal,bGroupByBytes);
strcat(szBuff,"\n");
puts(szBuff);
}
void
sprintbin32Info(char * szBuff,
const uint32_t ulVal,
const unsigned short uhb,
const unsigned short ulb)
{
sprintbin32(szBuff, ulVal, 1);
szBuff += 32 + 3;
*(szBuff++) = '\n';
*szBuff = '\0';
sprintf(szBuff,
"%02d....%02d %02d....%02d %02d....%02d %02d....%02d",
uhb,
3 * (uhb + ulb + 1) / 4, 3 * ( (uhb + ulb + 1) / 4) - 1,
(uhb + ulb + 1) / 2, ( (uhb + ulb + 1) / 2) - 1,
(uhb + ulb + 1) / 4, ( (uhb + ulb + 1) / 4) - 1,
ulb);
}
void
printbin32Info( const uint32_t ulVal,
const unsigned short uhb,
const unsigned short ulb)
{
char szBuff[2 * (32 + 3) + 2 + 1];
sprintbin32Info(szBuff, ulVal, uhb, ulb);
strcat(szBuff,"\n");
puts(szBuff);
}
uint32_t
get_notnull_random()
{
uint32_t val;
do
val = ((uint32_t)random() << 1) ^ (uint32_t)random();
while(!val);
return val;
}
uint32_t
get_notnull_random_balanced()
{
uint32_t val;
do
val = ((uint32_t)random() << 1) ^ (uint32_t)random();
while(count_bit32(val) != 16);
return val;
}
uint8_t
get_notnull8_random_balanced()
{
uint8_t val;
static uint8_t prev = 0;
do
val = (uint8_t)random();
while((count_bit8(val) != 4) || (val == prev));
prev = val;
return val;
}
uint32_t
get_notnull_random_balanced_mByte()
{
uint32_t val;
static uint32_t prev = 0;
do
val = (get_notnull8_random_balanced() << 24)
| (get_notnull8_random_balanced() << 16)
| (get_notnull8_random_balanced() << 8)
| (get_notnull8_random_balanced() << 0);
while(val == prev);
prev = val;
return val;
}