commit a3aacba73db2be5e96676549cd77ce8e67caa343 Author: cinqatte Date: Fri Jul 12 20:02:54 2024 +0200 initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..af9fe71 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +cmake-build-debug/ +cmake-build-release/ +build/ +lib/ +.idea/ +.vs/ +.vscode/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..76884f5 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.10) +project(numa VERSION 0.0.0 LANGUAGES C) + +set(CMAKE_CXX_STANDARD 99) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib) + +add_library(numa STATIC src/numa.c) + +target_include_directories(numa PUBLIC include) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..444383e --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ + + +Copyright 2024 Cinqatte + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/include/numa/numa.h b/include/numa/numa.h new file mode 100644 index 0000000..78f092a --- /dev/null +++ b/include/numa/numa.h @@ -0,0 +1,57 @@ +#ifndef NUMA_NUMA_H +#define NUMA_NUMA_H + +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 + +#include +#define numaDisplayClear() system("cls") +#define numaSleep(ms) Sleep(ms) + +#else + +#include +#define numaDisplayClear() system("clear") +#define numaSleep(ms) usleep(ms * 1000) + +#endif + +/** + * @brief Structure to hold the configuration for the Numa display. + * + * @param start_address Starting memory address to display. + * @param length Length of the memory to display in bytes. + * @param width Number of bytes to display per line. + * @param milliseconds Delay in milliseconds between updates. + * @param update Flag to determine if the display should continuously update. + * @param bytes_per_view Number of bytes to display together (1 for single byte, 2 for 2 bytes, etc.). + */ +__attribute__((unused)) typedef struct NumaDisplay { + uint64_t start_address; + uint32_t length; + uint32_t width; + uint32_t milliseconds; + bool update; + uint8_t bytes_per_view; +} NumaDisplay; + + +/** + * @brief Displays the memory content in a formatted view. + * + * @param display Pointer to the NumaDisplay structure containing the display configuration. + * + * This function clears the screen, prints the header, and then displays the memory content + * starting from `display->start_address` up to the specified `display->length` in a formatted view. + * The display is updated continuously if `display->update` is true, with a delay specified by + * `display->milliseconds`. + */ +__attribute__((unused)) void numaDisplay(NumaDisplay * display); + +#endif /* NUMA_NUMA_H */ diff --git a/src/numa.c b/src/numa.c new file mode 100644 index 0000000..8bbd1bd --- /dev/null +++ b/src/numa.c @@ -0,0 +1,73 @@ +#include "numa/numa.h" + +__attribute__((unused)) static void printHeader(uint32_t width, uint8_t bytes_per_view) { + printf("Address "); + for (uint32_t i = 0; i < width; i += bytes_per_view) { + for (uint32_t j = 0; j < bytes_per_view; j++) { + printf("%02X", j); + } + printf(" "); + if ((i + bytes_per_view) % 8 == 0) { + printf(" "); + } + } + printf(" ASCII\n"); +} + +__attribute__((unused)) void numaDisplay(NumaDisplay *display) { + uint64_t end_address = display->start_address + display->length; + uint64_t padded_end_address = end_address; + + if (display->length % display->width != 0) { + padded_end_address += display->width - (display->length % display->width); + } + + do { + numaDisplayClear(); + printHeader(display->width, display->bytes_per_view); + + for (uint64_t address = display->start_address; address < padded_end_address; address += display->width) { + printf("%08lX ", address); + + for (uint32_t i = 0; i < display->width; i += display->bytes_per_view) { + if (address + i < end_address) { + for (uint32_t j = 0; j < display->bytes_per_view; j++) { + if (address + i + j < end_address) { + printf("%02X", *((uint8_t *)(address + i + j))); + } else { + printf(" "); + } + } + printf(" "); + if ((i + display->bytes_per_view) % 8 == 0 && i != 0) { + printf(" "); + } + } else { + for (uint32_t j = 0; j < display->bytes_per_view; j++) { + printf(" "); + } + printf(" "); + } + } + + printf(" "); + + for (uint32_t i = 0; i < display->width; i++) { + if (address + i < end_address) { + char c = *((char *)(address + i)); + printf("%c", (c >= 32 && c <= 126) ? c : '.'); + } else { + printf(" "); + } + if ((i + 1) % 8 == 0 && i != 0) { + printf(" "); + } + } + printf("\n"); + } + + if (display->milliseconds > 0) { + numaSleep(display->milliseconds); + } + } while (display->update); +} \ No newline at end of file