commit 71b57be738098d2761777025ad3286ef6ed86e7a
parent 7ca6c6fbca0f472aac50d020ec622c7ec5f66ca7
Author: Christos Margiolis <christos@margiolis.net>
Date: Wed, 19 May 2021 18:31:06 +0300
finished VHDL ex3
Diffstat:
13 files changed, 178 insertions(+), 106 deletions(-)
diff --git a/c-os2/ex2/ex3_client.c b/c-os2/ex2/ex3_client.c
@@ -39,7 +39,7 @@ emalloc(size_t nb)
static void
die(const char *str)
{
- (void)fprintf(stderr, "%s: ", argv0);
+ fprintf(stderr, "%s: ", argv0);
perror(str);
exit(1);
}
diff --git a/c-os2/ex2/ex3_server.c b/c-os2/ex2/ex3_server.c
@@ -33,6 +33,7 @@ static void *emalloc(size_t);
static void die(const char *);
static char *argv0;
+/* Only gets set if a termination signal is caught. */
static volatile sig_atomic_t f_quit = 0;
static int
@@ -113,10 +114,11 @@ emalloc(size_t nb)
return p;
}
+/* FIXME: clean up resources as well. */
static void
die(const char *str)
{
- (void)fprintf(stderr, "%s: ", argv0);
+ fprintf(stderr, "%s: ", argv0);
perror(str);
exit(1);
}
@@ -124,15 +126,15 @@ die(const char *str)
static void
usage(void)
{
- (void)fprintf(stderr, "usage: %s [-b backlog] [-s sockfile]\n", argv0);
+ fprintf(stderr, "usage: %s [-b backlog] [-s sockfile]\n", argv0);
exit(1);
}
int
main(int argc, char *argv[])
{
- struct sockaddr_un sun;
struct foo *f;
+ struct sockaddr_un sun;
struct sigaction sa;
char *sockfile = "/tmp/cool.sock";
int sfd;
@@ -142,7 +144,15 @@ main(int argc, char *argv[])
argv0 = *argv;
if ((ch = getopt(argc, argv, "b:s:")) != -1) {
switch (ch) {
- case 'b';
+ case 'b':
+ /*
+ * Negative `backlog` value normally requests the
+ * maximum allowable value (HISTORY section of
+ * listen(2)'s FreeBSD man page), but it's better to
+ * not allow it in case the user passes a negative
+ * value accidentally. Also a value of 0 doesn't make
+ * any sense, so we don't allow it either.
+ */
if ((backlog = atoi(optarg)) < 1)
usage();
break;
@@ -188,7 +198,7 @@ main(int argc, char *argv[])
f->ntotal = 0;
for (;;) {
- /* FIXME: blocked by accept */
+ /* FIXME: blocked by accept(2) */
if (f_quit)
break;
/*
@@ -217,7 +227,8 @@ main(int argc, char *argv[])
free(f);
/*
* bind(2)'s man page states that the socket should be deleted when
- * it's no longer needed.
+ * it's no longer needed, otherwise it'll stay there even after
+ * we exit.
*/
(void)unlink(sockfile);
diff --git a/c-os2/lab2_pipe.c b/c-os2/lab2_pipe.c
@@ -14,7 +14,7 @@ static char *argv0;
static void
die(const char *str)
{
- (void)fprintf(stderr, "%s: ", argv0);
+ fprintf(stderr, "%s: ", argv0);
perror(str);
exit(1);
}
@@ -29,7 +29,7 @@ main(int argc, char *argv[])
argv0 = *argv;
if (argc != 2) {
- (void)fprintf(stderr, "usage: %s file\n", argv0);
+ fprintf(stderr, "usage: %s file\n", argv0);
return 1;
}
if ((fp = fopen(argv[1], "r")) == NULL)
diff --git a/cpp-oop/game/Engine.cc b/cpp-oop/game/Engine.cc
@@ -9,7 +9,6 @@ enum Color {
LAST
};
-
Engine::Engine()
{
}
@@ -34,84 +33,6 @@ Engine::init(const char *mapfile, const char *scorefile)
if (!init_score(scorefile))
throw "init_score failed: " + std::string(scorefile);
f_running = 1;
-
- /* Initialize player */
-}
-
-bool
-Engine::load_map(const char *mapfile)
-{
- std::ifstream f;
- std::vector<char> row;
- char c;
-
- f.exceptions(std::ifstream::badbit);
- f.open(mapfile);
- if (!f.is_open())
- return false;
- while (f.get(c)) {
- if (f.eof())
- break;
- row.push_back(c);
- if (c == '\n') {
- map.push_back(row);
- row.clear();
- }
- }
- f.close();
-
- return true;
-}
-
-bool
-Engine::init_curses()
-{
- /* Initialize curses(3) environment */
- if (!initscr())
- return false;
- noecho();
- cbreak();
- curs_set(false);
- keypad(stdscr, true);
- set_escdelay(0);
- /* TODO: make it async */
-
- xmax = getmaxx(stdscr);
- ymax = getmaxy(stdscr);
-
- /* Enable and initialize color pairs */
- colors.push_back(COLOR_BLUE); /* Wall */
- colors.push_back(COLOR_GREEN); /* Path */
- colors.push_back(COLOR_MAGENTA); /* Potter */
- colors.push_back(COLOR_CYAN); /* Gnome */
- colors.push_back(COLOR_YELLOW); /* Traal */
-
- start_color();
- use_default_colors();
- for (int i = 1; i < Color::LAST; i++)
- (void)init_pair(i, colors[i-1], -1);
-
- return true;
-}
-
-bool
-Engine::init_entities()
-{
- srand(time(nullptr));
-
- entities.push_back(new Potter(15, 10, Movable::Direction::DOWN, 'P'));
- entities.push_back(new Gnome(30, 20, Movable::Direction::DOWN, 'G'));
- entities.push_back(new Traal(50, 26, Movable::Direction::DOWN, 'T'));
-
- player = (Potter *)entities[0];
-
- return true;
-}
-
-bool
-Engine::init_score(const char *scorefile)
-{
- return true;
}
void
@@ -132,8 +53,12 @@ Engine::kbd_input()
case KEY_DOWN:
dir = Movable::Direction::DOWN;
break;
- case 'q':
+ case 'c':
+ menuopts();
+ return;
+ case ESC: /* FALLTHROUGH */
f_running = 0;
+ default:
return;
}
@@ -153,11 +78,13 @@ Engine::upd_score()
void
Engine::redraw()
{
+ char msg_opts[] = "c Controls";
int color;
erase();
/* TODO: add scores and stuff */
printw("(%d, %d)", player->get_x(), player->get_y());
+ mvprintw(0, xmax - strlen(msg_opts), msg_opts);
mvhline(1, 0, ACS_HLINE, xmax);
move (2, 0);
attron(A_REVERSE);
@@ -194,3 +121,106 @@ Engine::is_running()
{
return f_running;
}
+
+bool
+Engine::load_map(const char *mapfile)
+{
+ std::ifstream f;
+ std::vector<char> row;
+ char c;
+
+ f.exceptions(std::ifstream::badbit);
+ f.open(mapfile);
+ if (!f.is_open())
+ return false;
+ while (f.get(c)) {
+ if (f.eof())
+ break;
+ row.push_back(c);
+ if (c == '\n') {
+ map.push_back(row);
+ row.clear();
+ }
+ }
+ f.close();
+
+ return true;
+}
+
+/* Initialize curses(3) environment */
+bool
+Engine::init_curses()
+{
+ if (!initscr())
+ return false;
+ noecho();
+ cbreak();
+ curs_set(false);
+ keypad(stdscr, true);
+ set_escdelay(0);
+
+ xmax = getmaxx(stdscr);
+ ymax = getmaxy(stdscr);
+
+ colors.push_back(COLOR_BLUE); /* Wall */
+ colors.push_back(COLOR_GREEN); /* Path */
+ colors.push_back(COLOR_MAGENTA);/* Potter */
+ colors.push_back(COLOR_CYAN); /* Gnome */
+ colors.push_back(COLOR_YELLOW); /* Traal */
+
+ start_color();
+ use_default_colors();
+ for (int i = 1; i < Color::LAST; i++)
+ (void)init_pair(i, colors[i-1], -1);
+
+ return true;
+}
+
+bool
+Engine::init_entities()
+{
+ srand(time(nullptr));
+
+ entities.push_back(new Potter(15, 10, Movable::Direction::DOWN, 'P'));
+ entities.push_back(new Gnome(30, 20, Movable::Direction::DOWN, 'G'));
+ entities.push_back(new Traal(50, 26, Movable::Direction::DOWN, 'T'));
+
+ player = (Potter *)entities[0];
+
+ return true;
+}
+
+bool
+Engine::init_score(const char *scorefile)
+{
+ return true;
+}
+
+void
+Engine::menuopts()
+{
+ WINDOW *opts;
+ int w, h, wy, wx;
+
+ w = 32;
+ h = 9;
+ wy = CENTER(ymax, h);
+ wx = CENTER(xmax, w);
+ if ((opts = newwin(h, w, wy, wx)) == NULL)
+ return;
+ werase(opts);
+ box(opts, 0, 0);
+
+ mvwprintw(opts, 1, 1, "Up Move up");
+ mvwprintw(opts, 2, 1, "Down Move down");
+ mvwprintw(opts, 3, 1, "Left Move left");
+ mvwprintw(opts, 4, 1, "Right Move right");
+ mvwprintw(opts, 5, 1, "ESC Quit");
+ mvwprintw(opts, 7, 1, "Press any key to quit the menu");
+
+ wrefresh(opts);
+ (void)wgetch(opts);
+ werase(opts);
+ wrefresh(opts);
+ (void)delwin(opts);
+}
diff --git a/cpp-oop/game/Engine.hpp b/cpp-oop/game/Engine.hpp
@@ -15,6 +15,12 @@
#include "Traal.hpp"
#include "Score.hpp"
+#ifndef ESC
+#define ESC 27
+#endif /* ESC */
+
+#define CENTER(x, y) (((x) >> 1) - ((y) >> 1))
+
class Engine {
private:
@@ -43,6 +49,7 @@ private:
bool init_curses();
bool init_entities();
bool init_score(const char *scorefile);
+ void menuopts();
};
#endif /* _ENGINE_HPP_ */
diff --git a/cpp-oop/game/Score.cc b/cpp-oop/game/Score.cc
@@ -1 +1,11 @@
+#include <cstring>
#include "Score.hpp"
+
+Score::Score()
+{
+ (void)memset(&hiscores, 0, sizeof(hiscores));
+}
+
+Score::~Score()
+{
+}
diff --git a/cpp-oop/game/Score.hpp b/cpp-oop/game/Score.hpp
@@ -2,6 +2,15 @@
#define _SCORE_HPP_
class Score {
+private:
+ struct HighScores {
+ char name[10];
+ int score;
+ } hiscores[5];
+
+public:
+ Score();
+ ~Score();
};
#endif /* _SCORE_HPP_ */
diff --git a/cpp-oop/game/main.cc b/cpp-oop/game/main.cc
@@ -1,3 +1,5 @@
+#include <cstring>
+
#include "Engine.hpp"
/* Function declarations */
diff --git a/vhdl-digital-design/Makefile b/vhdl-digital-design/Makefile
@@ -1,6 +1,6 @@
all:
- ghdl -a --ieee=synopsys ${IN}.vhd
- ghdl -e --ieee=synopsys ${IN}
+ ghdl -a ${IN}.vhd
+ ghdl -e ${IN}
ghdl -r ${IN} --vcd=${IN}.vcd
sim:
diff --git a/vhdl-digital-design/ex3/regfile.vhd b/vhdl-digital-design/ex3/regfile.vhd
@@ -32,4 +32,4 @@ begin
end if;
end process;
c <= regf(to_integer(unsigned(addr)));
-end behav;-
\ No newline at end of file
+end behav;
diff --git a/vhdl-digital-design/ex3/regfile_ext.vhd b/vhdl-digital-design/ex3/regfile_ext.vhd
@@ -42,4 +42,4 @@ begin
end process;
b <= regf(to_integer(unsigned(raddr1)));
c <= regf(to_integer(unsigned(raddr2)));
-end behav;-
\ No newline at end of file
+end behav;
diff --git a/vhdl-digital-design/ex3/regfile_ext_tb.vhd b/vhdl-digital-design/ex3/regfile_ext_tb.vhd
@@ -53,10 +53,15 @@ begin
process begin
s_we <= '1';
+ s_clk <= '1';
+ s_rst <= '1';
+ wait for 250 ns;
+
+ s_we <= '1';
s_clk <= '0';
s_rst <= '0';
s_raddr1 <= "00";
- s_raddr2 <= "01";
+ s_raddr2 <= "00";
s_waddr <= "00";
s_a <= "0101";
wait for 250 ns;
@@ -70,37 +75,37 @@ begin
s_clk <= '0';
s_rst <= '0';
s_raddr1 <= "01";
- s_raddr2 <= "10";
+ s_raddr2 <= "01";
s_waddr <= "01";
- s_a <= "1101";
+ s_a <= "1010";
wait for 250 ns;
s_we <= '1';
s_clk <= '1';
- s_rst <= '0';
+ s_rst <= '1';
wait for 250 ns;
- s_we <= '0';
+ s_we <= '1';
s_clk <= '0';
s_rst <= '0';
s_raddr1 <= "10";
- s_raddr2 <= "11";
+ s_raddr2 <= "10";
s_waddr <= "10";
- s_a <= "0010";
+ s_a <= "0000";
wait for 250 ns;
s_we <= '1';
s_clk <= '1';
- s_rst <= '0';
+ s_rst <= '1';
wait for 250 ns;
s_we <= '1';
s_clk <= '0';
s_rst <= '0';
s_raddr1 <= "11";
- s_raddr2 <= "01";
+ s_raddr2 <= "11";
s_waddr <= "11";
- s_a <= "1001";
+ s_a <= "1111";
wait for 250 ns;
end process;
end behav;
diff --git a/vhdl-digital-design/ex3/regfile_tb.vhd b/vhdl-digital-design/ex3/regfile_tb.vhd
@@ -64,7 +64,7 @@ begin
s_clk <= '1';
wait for 250 ns;
- s_we <= '0';
+ s_we <= '1';
s_clk <= '0';
s_addr <= "10";
s_a <= "0010";