Permalink
Browse files

Split system stop into reset and stop for simplicity

  • Loading branch information...
marciof committed Jun 9, 2018
1 parent ace6dfd commit 16dd4dcca10c9547cb5376df93c7298263a6b9c8
View
@@ -1,12 +1,11 @@
- Check CLang warnings?
- Check places where it assumed K_LOG_ERROR was stopping the system.
- Verify all uses of Err (where it's missing, where it's not needed, etc).
- Split "System.c" into "main.c"; also add a "main.c" in i386?
- Make Err be the 2nd param always for consistency.
- Don't access struct members directly? eg. `system->stop(system)`.
- Don't use wrappers to access struct members directly.
- How to add full stack trace information to `Err` instances?
- Make System/etc not be a singleton and also to make it easier to test. Receive dependencies, such as Log, through an init function.
- JS uses the same native System, since it's just a backend for code generation, just like a real-time interpreter is another backend. So don't merge it into native to allow an embedded systems to just pick core+native, but merge into core.
- JS uses the same native System, since it's just a backend for code generation, just like a real-time interpreter is another backend. So don't merge it into native to allow an embedded systems to just pick core+native, but merge JS-independent parts into core.
- Add Travis CI build matrix for the different builds (native, js, i386) and OSes.
- Regularly test building on Debian 32-bit 64-bit, Windows, OSX.
- Add simple v86 HTML demo?
View
@@ -10,11 +10,11 @@
#define K_BIT_IS_SET(value, bit_flag) \
(((value) & (bit_flag)) != 0 ? true : false)
#define K_BIT_ATTR_PACKED(type) \
#define K_BIT_PACKED(type) \
type __attribute__((__packed__))
#define K_BIT_ATTR_SECTION(name, declaration) \
#define K_BIT_SECTION(name, declaration) \
declaration __attribute__((section(name)))
#define K_BIT_ATTR_UNUSED(param) \
#define K_BIT_UNUSED(param) \
param __attribute__((unused))
View
@@ -3,7 +3,7 @@
#include "Log.h"
void k_Err_describe(struct k_Err* err) {
struct k_Err discard_log_err = K_ERR_NONE;
struct k_Err discard_log_err = K_ERR_INIT;
// FIXME: pass Log over
err->describe(err);
@@ -14,7 +14,7 @@ void k_Err_describe(struct k_Err* err) {
}
void k_Err_describe_text(struct k_Err* err) {
struct k_Err discard_log_err = K_ERR_NONE;
struct k_Err discard_log_err = K_ERR_INIT;
k_Log_msg(k_Log_get(), &discard_log_err, K_LOG_ERROR,
"{s}", (char*) err->arg);
@@ -24,6 +24,10 @@ bool k_Err_has(struct k_Err* err) {
return err->describe != NULL;
}
void k_Err_reset(struct k_Err* err) {
err->describe = NULL;
}
void k_Err_set(
struct k_Err* err,
void (*describe)(struct k_Err*),
View
@@ -3,7 +3,7 @@
#include <stddef.h>
#include <stdint.h>
#define K_ERR_NONE {NULL}
#define K_ERR_INIT {NULL}
struct k_Err {
/** Description ends in a newline. */
@@ -20,6 +20,8 @@ void k_Err_describe_text(struct k_Err* err);
bool k_Err_has(struct k_Err* err);
void k_Err_reset(struct k_Err* err);
void k_Err_set(
struct k_Err* err,
void (*describe)(struct k_Err*),
@@ -2,13 +2,9 @@
#include <stdint.h>
#include "Err.h"
enum k_System_Stop_Mode {K_SYSTEM_HALT, K_SYSTEM_RESET};
struct k_System {
void (*stop)(
struct k_System* system,
struct k_Err* err,
enum k_System_Stop_Mode mode);
void (*reset)(struct k_System* system, struct k_Err* err);
void (*stop)(struct k_System* system, struct k_Err* err);
uint8_t val[];
};
View
@@ -3,8 +3,8 @@
#include "vga/Text.h"
static void prepare(
K_BIT_ATTR_UNUSED(struct k_Log* log),
struct k_Err* err,
K_BIT_UNUSED(struct k_Log* log),
K_BIT_UNUSED(struct k_Err* err),
enum k_Log_Level lvl) {
if (lvl == K_LOG_ERROR) {
@@ -19,16 +19,16 @@ static void prepare(
}
static void print_ch(
K_BIT_ATTR_UNUSED(struct k_Log* log),
struct k_Err* err,
K_BIT_UNUSED(struct k_Log* log),
K_BIT_UNUSED(struct k_Err* err),
char ch) {
k_VGA_Text_print_ch(ch);
}
static void print_str(
K_BIT_ATTR_UNUSED(struct k_Log* log),
struct k_Err* err,
K_BIT_UNUSED(struct k_Log* log),
K_BIT_UNUSED(struct k_Err* err),
const char* str) {
k_VGA_Text_print_str(str);
@@ -50,7 +50,7 @@ enum Segment_Type {
CODE_DATA = 1,
};
K_BIT_ATTR_PACKED(struct Segment_Descriptor {
K_BIT_PACKED(struct Segment_Descriptor {
/** Specifies the segment size according to the granularity. */
uint16_t segment_limit_low;
@@ -19,7 +19,7 @@ enum {
DRIVE_FIRST_HARD_DISK_DRIVE = 0x80,
};
K_BIT_ATTR_PACKED(struct Drive {
K_BIT_PACKED(struct Drive {
/** Size doesn't equal `10 + 2 * num. ports` due to alignment. */
uint32_t size;
@@ -37,7 +37,7 @@ K_BIT_ATTR_PACKED(struct Drive {
uint16_t* ports;
});
K_BIT_ATTR_PACKED(struct Boot_Device {
K_BIT_PACKED(struct Boot_Device {
uint8_t sub_sub_partition;
uint8_t sub_partition;
uint8_t top_level_partition;
@@ -50,7 +50,7 @@ enum {BOOT_DEVICE_UNUSED_PARTITION = 0xFF};
extern struct multiboot_info* k_Multiboot_info;
extern uint32_t k_Multiboot_magic_num;
K_BIT_ATTR_SECTION(
K_BIT_SECTION(
".multiboot_header",
const struct multiboot_header k_Multiboot_header) = {
MULTIBOOT_HEADER_MAGIC,
@@ -96,7 +96,7 @@ static void log_boot_modules(
struct multiboot_mod_list* module = &modules[i];
k_Log_msg(log, err, K_LOG_INFO,
"Boot module: start={iuh}; end={iuh}; string=\"{s}\"",
"Boot module: start={iuh}; end={iuh}; string='{s}'",
module->mod_start, module->mod_end, (char*) module->cmdline);
if (k_Err_has(err)) {
@@ -234,7 +234,7 @@ struct multiboot_info* k_Multiboot_get_info(struct k_Err* err) {
{
k_Log_msg(k_Log_get(), err, K_LOG_ERROR,
"Invalid Multiboot information: "
"Both bits 4 and 5 of the flags field are set.");
"Both bits 4 and 5 of the flags field are set");
if (k_Err_has(err)) {
return NULL;
@@ -269,7 +269,7 @@ void k_Multiboot_log_info(
}
if (K_BIT_IS_SET(info->flags, MULTIBOOT_INFO_CMDLINE)) {
k_Log_msg(log, err, K_LOG_INFO, "Command line: \"{s}\"", info->cmdline);
k_Log_msg(log, err, K_LOG_INFO, "Command line: '{s}'", info->cmdline);
if (k_Err_has(err)) {
return;
}
@@ -304,7 +304,7 @@ void k_Multiboot_log_info(
}
if (K_BIT_IS_SET(info->flags, MULTIBOOT_INFO_BOOT_LOADER_NAME)) {
k_Log_msg(log, err, K_LOG_INFO, "Boot loader: \"{s}\"",
k_Log_msg(log, err, K_LOG_INFO, "Boot loader: {s}",
info->boot_loader_name);
if (k_Err_has(err)) {
return;
View
@@ -6,49 +6,55 @@
#include "Multiboot.h"
#include "vga/Text.h"
extern void k_System_halt(void);
extern void k_System_reset(void);
void main(void) {
struct k_Err err = K_ERR_NONE;
struct k_Err err = K_ERR_INIT;
k_VGA_Text_init();
// FIXME: move logging and other dependencies to k_System_start()
// FIXME: move logging and other dependencies to a k_System_start?
struct multiboot_info* multiboot_info = k_Multiboot_get_info(&err);
if (!k_Err_has(&err)) {
k_Multiboot_log_info(multiboot_info, &err, k_Log_get());
}
struct k_System* system = k_System_get();
system->stop(system, &err, K_SYSTEM_HALT);
system->stop(system, &err);
}
extern void k_System_halt(void);
static void reset(
K_BIT_UNUSED(struct k_System* system),
K_BIT_UNUSED(struct k_Err* err)) {
extern void k_System_reset(void);
if (k_Err_has(err)) {
k_Err_describe(err);
}
k_System_reset();
}
static void stop(
K_BIT_ATTR_UNUSED(struct k_System* system),
struct k_Err* err,
enum k_System_Stop_Mode mode) {
switch (mode) {
case K_SYSTEM_HALT:
// FIXME: implement halt shutdown mode
k_Log_msg(k_Log_get(), err, K_LOG_WARN, "Halt shutdown not implemented.");
if (k_Err_has(err)) {
k_Err_describe(err);
}
k_System_halt();
break;
case K_SYSTEM_RESET:
k_System_reset();
break;
default:
k_Log_msg(k_Log_get(), err, K_LOG_ERROR, "Invalid system stop mode.");
break;
K_BIT_UNUSED(struct k_System* system),
struct k_Err* err) {
if (k_Err_has(err)) {
k_Err_describe(err);
k_Err_reset(err);
}
// FIXME: implement shutdown
k_Log_msg(k_Log_get(), err, K_LOG_WARN, "Shutdown not implemented");
if (k_Err_has(err)) {
k_Err_describe(err);
}
k_System_halt();
}
struct k_System* k_System_get(void) {
static struct k_System system = {stop};
static struct k_System system = {reset, stop};
return &system;
}
View
@@ -25,7 +25,7 @@ static void increase_max_len(struct k_Str* str, struct k_Err* err) {
str->max_len = new_max_len;
}
static void Str_free(void* str) {
static void free_str(void* str) {
free(((struct k_Str*) str)->val);
free(str);
}
@@ -62,5 +62,5 @@ struct k_Str* k_Str_new(struct k_Err* err) {
str->val = val;
str->len = 0;
str->max_len = max_len;
return K_REF_INIT(str, Str_free);
return K_REF_INIT(str, free_str);
}
View
@@ -4,8 +4,8 @@
#include "../core/Log.h"
#include "Token.h"
static void Token_describe_err(struct k_Err* err) {
struct k_Err discard_log_err = K_ERR_NONE;
static void describe_token_err(struct k_Err* err) {
struct k_Err discard_log_err = K_ERR_INIT;
struct k_Input* input = (struct k_Input*) err->arg;
int ch = input->peek_ch(input, &discard_log_err);
@@ -15,14 +15,14 @@ static void Token_describe_err(struct k_Err* err) {
(char) ch, input->location, input->line, input->column);
}
static void Token_free(void* ptr) {
static void free_token(void* ptr) {
struct k_Token* token = ptr;
K_REF_DEC(token->str);
K_REF_DEC(token->input);
free(ptr);
}
static struct k_Token* Token_new(
static struct k_Token* new_token(
enum k_Token_Type type,
struct k_Str* str,
struct k_Input* input,
@@ -43,7 +43,7 @@ static struct k_Token* Token_new(
token->line = line;
token->column = column;
return K_REF_INIT(token, Token_free);
return K_REF_INIT(token, free_token);
}
static struct k_Str* read_comment(struct k_Input* input, struct k_Err* err) {
@@ -132,11 +132,11 @@ struct k_Token* k_Token_parse(struct k_Input* input, struct k_Err* err) {
type = K_TOKEN_WHITESPACE;
}
else {
K_ERR_SET(err, Token_describe_err, input);
K_ERR_SET(err, describe_token_err, input);
return NULL;
}
struct k_Token* token = Token_new(type, str, input, line, column, err);
struct k_Token* token = new_token(type, str, input, line, column, err);
K_REF_DEC(str);
return token;
}
View
@@ -6,7 +6,7 @@
#include "Token.h"
int main(void) {
struct k_Err err = K_ERR_NONE;
struct k_Err err = K_ERR_INIT;
struct k_Input* stdin_input = k_Input_from_fd(
STDIN_FILENO, "<stdin>", &err);
View
@@ -1,4 +1,5 @@
#include <stdio.h>
#include "../core/Bit.h"
#include "../core/Log.h"
struct Native_Log {
@@ -8,7 +9,7 @@ struct Native_Log {
// FIXME: add colors
static void prepare(
struct k_Log* log,
struct k_Err* err,
K_BIT_UNUSED(struct k_Err* err),
enum k_Log_Level lvl) {
struct Native_Log* native_log = (struct Native_Log*) log->val;
@@ -20,12 +21,20 @@ static void prepare(
}
// FIXME: error handling fputc
static void print_ch(struct k_Log* log, struct k_Err* err, char ch) {
static void print_ch(
struct k_Log* log,
K_BIT_UNUSED(struct k_Err* err),
char ch) {
fputc(ch, ((struct Native_Log*) log->val)->stream);
}
// FIXME: error handling fputs
static void print_str(struct k_Log* log, struct k_Err* err, const char* str) {
static void print_str(
struct k_Log* log,
K_BIT_UNUSED(struct k_Err* err),
const char* str) {
fputs(str, ((struct Native_Log*) log->val)->stream);
}
Oops, something went wrong.

0 comments on commit 16dd4dc

Please sign in to comment.