<commit_before>/*
 * raspberry.c -- Raspberry Pi kernel routines written in C
 *
 * Copyright 2014 <NAME>, <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "raspi.h"
#include "timer.h"
#include "serial.h"
#include "xmodem.h"

extern void mycelia();

/* Exported procedures (force full register discipline) */
extern void k_start(u32 sp);
extern void monitor();
extern int putchar(int c);
extern int getchar();
extern void hexdump(const u8* p, int n);
extern void dump256(const u8* p);

/* Private data structures */
static char linebuf[256];  // line editing buffer
static int linepos = 0;  // read position
static int linelen = 0;  // write position
static const char* hex = "0123456789abcdef";  // hexadecimal map

/*
 * Print u32 in hexadecimal to serial port
 */
void
serial_hex32(u32 w)
{
    serial_write(hex[0xF & (w >> 28)]);
    serial_write(hex[0xF & (w >> 24)]);
    serial_write(hex[0xF & (w >> 20)]);
    serial_write(hex[0xF & (w >> 16)]);
    serial_write(hex[0xF & (w >> 12)]);
    serial_write(hex[0xF & (w >> 8)]);
    serial_write(hex[0xF & (w >> 4)]);
    serial_write(hex[0xF & w]);
}

/*
 * Print u8 in hexadecimal to serial port
 */
void
serial_hex8(u8 b)
{
    serial_write(hex[0xF & (b >> 4)]);
    serial_write(hex[0xF & b]);
}

/*
 * Pretty-printed memory dump
 */
void
hexdump(const u8* p, int n)
{
    int i;
    int c;

    while (n > 0) {
        serial_hex32((u32)p);
        serial_write(' ');
        for (i = 0; i < 16; ++i) {
            if (i == 8) {
                serial_write(' ');
            }
            if (i < n) {
                serial_write(' ');
                serial_hex8(p[i]);
            } else {
                serial_rep(' ', 3);
            }
        }
        serial_rep(' ', 2);
        serial_write('|');
        for (i = 0; i < 16; ++i) {
            if (i < n) {
                c = p[i];
                if ((c >= ' ') && (c < 0x7F)) {
                    serial_write(c);
                } else {
                    serial_write('.');
                }
            } else {
                serial_write(' ');
            }
        }
        serial_write('|');
        serial_eol();
        p += 16;
        n -= 16;
    }
}

/*
 * Dump 256 bytes (handy for asm debugging, just load r0)
 */
void
dump256(const u8* p)
{
    hexdump(p, 256);
}

/*
@ 12345678 12345678 12345678 12345678 12345678 12345678 12345678 12345678
 \_ 12345678 12345678 12345678 12345678 12345678 12345678 12345678 12345678
*/
void
dump_block(const u32* p)
{
    int i;

    for (i = 0; i < 8; ++i) {
        serial_write(' ');
        serial_hex32(*p++);
    }
}
void
dump_event(const u32* p)
{
    serial_write('@');
    dump_block(p);
    serial_eol();
    if (((*p) > 0x8000) && ((*p) < 0x10000000)) {
        serial_write(' ');
        serial_write('\');
        serial_write('_');
        dump_block((u32*)(*p));
        serial_eol();
    }
}

/*<commit_msg>Escape backslash<commit_after>/*
 * raspberry.c -- Raspberry Pi kernel routines written in C
 *
 * Copyright 2014 <NAME>, <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "raspi.h"
#include "timer.h"
#include "serial.h"
#include "xmodem.h"

extern void mycelia();

/* Exported procedures (force full register discipline) */
extern void k_start(u32 sp);
extern void monitor();
extern int putchar(int c);
extern int getchar();
extern void hexdump(const u8* p, int n);
extern void dump256(const u8* p);

/* Private data structures */
static char linebuf[256];  // line editing buffer
static int linepos = 0;  // read position
static int linelen = 0;  // write position
static const char* hex = "0123456789abcdef";  // hexadecimal map

/*
 * Print u32 in hexadecimal to serial port
 */
void
serial_hex32(u32 w)
{
    serial_write(hex[0xF & (w >> 28)]);
    serial_write(hex[0xF & (w >> 24)]);
    serial_write(hex[0xF & (w >> 20)]);
    serial_write(hex[0xF & (w >> 16)]);
    serial_write(hex[0xF & (w >> 12)]);
    serial_write(hex[0xF & (w >> 8)]);
    serial_write(hex[0xF & (w >> 4)]);
    serial_write(hex[0xF & w]);
}

/*
 * Print u8 in hexadecimal to serial port
 */
void
serial_hex8(u8 b)
{
    serial_write(hex[0xF & (b >> 4)]);
    serial_write(hex[0xF & b]);
}

/*
 * Pretty-printed memory dump
 */
void
hexdump(const u8* p, int n)
{
    int i;
    int c;

    while (n > 0) {
        serial_hex32((u32)p);
        serial_write(' ');
        for (i = 0; i < 16; ++i) {
            if (i == 8) {
                serial_write(' ');
            }
            if (i < n) {
                serial_write(' ');
                serial_hex8(p[i]);
            } else {
                serial_rep(' ', 3);
            }
        }
        serial_rep(' ', 2);
        serial_write('|');
        for (i = 0; i < 16; ++i) {
            if (i < n) {
                c = p[i];
                if ((c >= ' ') && (c < 0x7F)) {
                    serial_write(c);
                } else {
                    serial_write('.');
                }
            } else {
                serial_write(' ');
            }
        }
        serial_write('|');
        serial_eol();
        p += 16;
        n -= 16;
    }
}

/*
 * Dump 256 bytes (handy for asm debugging, just load r0)
 */
void
dump256(const u8* p)
{
    hexdump(p, 256);
}

/*
@ 12345678 12345678 12345678 12345678 12345678 12345678 12345678 12345678
 \_ 12345678 12345678 12345678 12345678 12345678 12345678 12345678 12345678
*/
void
dump_block(const u32* p)
{
    int i;

    for (i = 0; i < 8; ++i) {
        serial_write(' ');
        serial_hex32(*p++);
    }
}
void
dump_event(const u32* p)
{
    serial_write('@');
    dump_block(p);
    serial_eol();
    if (((*p) > 0x8000) && ((*p) < 0x10000000)) {
        serial_write(' ');
        serial_write('\\');
        serial_write('_');
        dump_block((u32*)(*p));
        serial_eol();
    }
}

/*
{'fraction_non_alphanumeric': 0.14603960396039603, 'fraction_numerical': 0.07192778101339546, 'mean_word_length': 2.169820027688048, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6361873', 'n_tokens_mistral': 2709, 'n_tokens_neox': 2307, 'n_words': 677}