mirror of
https://github.com/Alvin-Zilverstand/bob16.git
synced 2026-11-22 21:13:00 +01:00
s
This commit is contained in:
1 parent
74bda7d8b2
commit
472df35261
23 files changed
+32910
-42
No files matched your search
@@ -0,0 +1,69 @@
|
||||
int heap_used;
|
||||
int bit_masks[16] = {-32768,16384,8192,4096,2048,1024,512,256,128,64,32,16,8,4,2,1};
|
||||
int bit_weights[4] = {8,4,2,1};
|
||||
|
||||
int strlen(char *text) { int n; n = 0; while (text[n]) n++; return n; }
|
||||
int strcmp(char *a, char *b) {
|
||||
int i; i = 0;
|
||||
while (a[i] && a[i] == b[i]) i++;
|
||||
return a[i] - b[i];
|
||||
}
|
||||
void memcpy(int *dst, int *src, int count) {
|
||||
int i; for (i = 0; i < count; i++) dst[i] = src[i];
|
||||
}
|
||||
void memset(int *dst, int value, int count) {
|
||||
int i; for (i = 0; i < count; i++) dst[i] = value;
|
||||
}
|
||||
void print(char *text) { int i; i = 0; while (text[i]) { bob_putc(text[i]); i++; } }
|
||||
void println(char *text) { print(text); bob_putc('\n'); }
|
||||
void decimal_negative(int number) {
|
||||
int quotient; int digit;
|
||||
quotient = number / 10;
|
||||
digit = -(number - quotient * 10);
|
||||
if (quotient) decimal_negative(quotient);
|
||||
bob_putc('0' + digit);
|
||||
}
|
||||
void print_dec(int number) {
|
||||
if (number < 0) bob_putc('-');
|
||||
else number = -number;
|
||||
decimal_negative(number);
|
||||
}
|
||||
void print_hex(int number) {
|
||||
int nibble; int bit; int digit;
|
||||
print("0x");
|
||||
for (nibble = 0; nibble < 4; nibble++) {
|
||||
digit = 0;
|
||||
for (bit = 0; bit < 4; bit++) {
|
||||
if (number & bit_masks[nibble * 4 + bit]) digit = digit + bit_weights[bit];
|
||||
}
|
||||
if (digit < 10) bob_putc('0' + digit);
|
||||
else bob_putc('A' + digit - 10);
|
||||
}
|
||||
}
|
||||
/* Consume the complete line, even when the destination fills. -2 means EOF. */
|
||||
int read_line(char *buffer, int capacity) {
|
||||
int length; int character; int truncated;
|
||||
if (capacity < 1) return -1;
|
||||
length = 0; truncated = 0;
|
||||
while (1) {
|
||||
character = bob_getc();
|
||||
if (character == -1 || character == '\n') break;
|
||||
if (character == '\b' || character == 127) { if (length) length--; }
|
||||
else if (character != '\r') {
|
||||
if (length < capacity - 1) buffer[length++] = character;
|
||||
else truncated = 1;
|
||||
}
|
||||
}
|
||||
buffer[length] = 0;
|
||||
if (truncated) return -1;
|
||||
if (character == -1 && length == 0) return -2;
|
||||
return length;
|
||||
}
|
||||
int *alloc(int words) {
|
||||
int *address;
|
||||
if (words < 1 || words > HEAP_WORDS - heap_used) return 0;
|
||||
address = (int *)(HEAP_BASE + heap_used);
|
||||
heap_used = heap_used + words;
|
||||
memset(address, 0, words);
|
||||
return address;
|
||||
}
|
||||
Reference in new issue
Block a user