Files
V2TMUMemTester/Utils.c
2026-03-09 01:10:56 +01:00

104 lines
2.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/>.
*/
#define _BSD_SOURCE 1
#include <stdlib.h>
#include <stdio.h>
#include "Utils.h"
void
printbin32( 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:
putchar(' ');
}
putchar(ulVal & idxBit ? '1' : '0');
}
}
void
printbin32Info( const uint32_t ulVal,
const unsigned short uhb,
const unsigned short ulb)
{
printbin32(ulVal,1);
putchar('\n');
printf("%02d....%02d %02d....%02d %02d....%02d %02d....%02d\n",
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);
}
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;
}