Add: program: words version 1

This commit is contained in:
xamidev
2024-07-24 15:08:39 +02:00
parent b352d3f4e2
commit 631099a53d
10 changed files with 104 additions and 10 deletions

View File

@@ -36,7 +36,7 @@ int kmain(int retvalue)
// TODO: Grub modules to load programs
//timer_install();
timer_install();
keyboard_install();
shell_install();

View File

@@ -1,6 +1,7 @@
#include "system.h"
#include "../libc/stdio.h"
#include "../libc/string.h"
#include "../programs/programs.h"
#define BUFFER_SIZE 256
@@ -26,6 +27,10 @@ void shell_install()
{
printf("%d", 4/0);
}
else if (strcmp(input_buffer, "words") == 0)
{
program_words();
}
else {
printf("Unknown command %s\n", input_buffer);
}

View File

@@ -22,5 +22,8 @@ void delay(int ticks);
void keyboard_install();
char keyboard_getchar();
void shell_install();
extern volatile unsigned long global_ticks;
#endif

View File

@@ -1,16 +1,11 @@
#include "system.h"
#include "../libc/stdio.h"
int timer_ticks = 0;
volatile unsigned long global_ticks = 0;
void timer_handler()
{
timer_ticks++;
if(timer_ticks % 18 == 0)
{
puts("One second has passed\n");
}
global_ticks++;
}
void timer_install()
@@ -21,6 +16,6 @@ void timer_install()
void delay(int ticks)
{
unsigned long eticks;
eticks = timer_ticks + ticks;
while ((unsigned long)timer_ticks < eticks);
eticks = global_ticks + ticks;
while (global_ticks < eticks);
}

View File

@@ -0,0 +1,24 @@
#include "crypto.h"
int lcg(int seed)
{
int x = seed;
// Constants (ZX81 LCG)
int a = 75;
int c = 74;
long m = 65537;
for (int i=0; i<10; i++)
{
x = (a*x + c) % m;
}
return x;
}
int randint(int seed)
{
int x = lcg(seed);
return x;
}

9
src/libc/crypto.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef CRYPTO_H
#define CRYPTO_H
#define RAND_MAX 1024
int lcg(int seed);
int randint(int seed);
#endif

6
src/programs/programs.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef PROGRAMS_H
#define PROGRAMS_H
void program_words();
#endif

52
src/programs/words.c Normal file
View File

@@ -0,0 +1,52 @@
#include "../libc/stdio.h"
#include "../libc/crypto.h"
#include "../kernel/system.h"
char* words[] =
{
"I", "us", "they", "my",
"a", "an", "is", "are", "for", "while", "not", "none", "yes", "no",
"absolutely", "addition", "additive", "afternoon", "architect", "ask",
"be", "blindfold", "brilliant", "boy", "brilliant", "bring", "buddy",
"career", "caterpillar", "change", "cheeky", "chop",
"decide", "demonstrate", "draw", "druggist",
"eagle", "ear", "effort", "evening",
"fabric", "famous", "fuse",
"generation", "generous", "girl", "gypsy", "grip",
"habit", "handsome", "helmet", "help", "horror",
"insist", "inventor", "itself", "ivory",
"jog", "joint", "joke", "judge",
"karate", "kebab", "kitchen",
"lamb", "lawnmower", "left", "lock",
"math", "medicine", "most",
"noodles", "nowadays", "nowhere",
"ocean", "older", "ounce",
"part", "pathetic", "pastime",
"quite", "quits", "quotation",
"race", "raise", "reality",
"safe", "scare", "screen",
"taught", "temple", "that", "this",
"unable", "unkind", "usual",
"velvet", "vivid", "vote",
"we", "warm", "watch",
"xylophone",
"yolk", "young", "your",
"zebra", "zodiac", "zucchini",
};
int words_size = sizeof(words)/sizeof(words[0]);
// Generates 5 random words
void program_words()
{
for (int i=0; i<10; i++)
{
int random = randint(global_ticks);
char* word = words[random%words_size];
//printf("Global ticks: %d\nRandom integer: %d\nWord: %s\n", global_ticks, random, word);
printf("%s ", word);
delay(10);
}
puts("\n");
}